- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements