- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access the members of a class from another class in Java?
To access the members of a class from other class.
- First of all, import the class.
- Create an object of that class.
- Using this object access, the members of that class.
Suppose there is a class in a package called myPackage with a method named display()
package myPackage; public class Sample { public void display() { System.out.println("Hello"); } }
You can access it as.
import myPackage; public class MyClass { public static void main(String args[]) { Sample s = new Sample(); s.done(); } }
Output
hello
- Related Articles
- How to access the private methods of a class from outside of the class in Java?
- How to access Static variable of Outer class from Static Inner class in java?
- How to access the object of a class without using the class name from a static context in java?
- What are static members of a Java class?
- How to access variables from enclosing class using lambda in Java?
- What are the steps to read static members in a Java class?
- How to convert a class to another class type in C++?
- Changing Class Members in Python?
- How to access Java package from another package
- What are static members of a C# Class?
- What are the differences between class methods and class members in C#?
- Accessing protected members in a C++ derived class
- How to access a derived class member variable by an interface object in Java?
- How to prevent object of a class from garbage collection in Java?
- Can private methods of a class be accessed from outside of a class in Java?

Advertisements