class A{
String z(){
System.out.println(\"a\");
return \"sauarbh\";
}
}
class B{
A a;
A x(){
return a;
}
}
public class runner
Yes without instantiate the class if you want to call the method you should use static
key word.
What are you doing here?
You are indirectly try to get instance of A
. But this case you will get NullPointerException
since you just return only a reference(variable) of A
B b = new B();
A a2=b.x();
a2.z(); // NullPointerException from here
NPE?
class B{
A a;
A x(){
return a; // you just return the reference
// it should be return new A();
}
}
For your edit:
Take a look at insert()
method. It is creating Person
instance.