java - What if I create an object of Class Two in Class One itself and then access Class Two's data...What will be the difference? -
here in code snippet of object relation, taking reference of class 2 in class one, , through accessing class two's members.
what if create class two's object in class 1 , access methods? difference in doing ?
i know missing concept here not getting it.
// object relation using references package object_relation; class 1 { // instance variables int x; 2 t; public one(two t) { this.t = t; x=10; } void display() { system.out.println("class 1 members : "); system.out.println("x = "+x); system.out.println("displaying class 2 members using method"); t.display(); system.out.println("displaying class 2 members using reference :"); system.out.println("y = "+t.y); } } class 2 { // instance variables int y; public two(int y) { this.y = y; } public void display() { system.out.println("y = "+y); } } public class usingreference { public static void main(string[] args) { 2 t2 = new two(20); 1 o = new one(t2); o.display(); } }
i not sure question is?
but here in example 1 , 2 both different classes. , in-order access non-static member variables need object of class two.
are looking concept inner class in java:- example
public class outerclass { int i; public void method1() { system.out.println("inside method 1"); } class innerclass { int j; public void method2() { system.out.println("inside method 2"); } } public static void main(string[] args) { // instantiate inner class, must first instantiate outer class outerclass outerobject = new outerclass(); outerobject.method1(); // then, create inner object within outer object outerclass.innerclass innerobject = outerobject.new innerclass(); innerobject.method2(); } }
ps: here innerclass member of outerclass.
Comments
Post a Comment