-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassObj1.java
More file actions
25 lines (19 loc) · 773 Bytes
/
ClassObj1.java
File metadata and controls
25 lines (19 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class ClassObj1{
String str = "Hi";
public static void main(String[] args) {
// Initialise object for that class
// from which you want to access its variable or method
// Accessing 'var' variable present in 'classobj2'
Classobj2 object = new Classobj2();
System.out.println("Variable var from outer class 'ClassObj2': "+ object.var);
// Accessing string 'str' present in 'Classobj1'
ClassObj1 obj = new ClassObj1();
System.out.println("String str from ClassObj1: " + obj.str);
}
}
class Classobj2{
// A class is not able to access fields of another class if
// fields are present inside main method
// Wrong: public static void main(String[] args) {
int var = 500;
}