Back into Java but i'm lost on some basic referencing
If i have for example: how do i refer to method test2 or test3 from within method problem within class b?
Any help would be greatly appreciated.
public class zz{
a1=new a();
// i can do
a1.b1.test();
public test2()
{
}
}
public class a{
b b1= new b()
public test3()
{
}
}
public class b{
public test()
{
}
public problem()
{
// but how do i refer to method test2 or test3 from here?
something like
zz.test2();
zz.a1.test3();
}
}
You can make method of class a static to invoke it like this :
a.test3();
or create instance of object a to invoke itlike this :
public problem {
a objA = new a();
a.test();
}
You need to create the object before using it to call the methods. You can only call static methods directly with the class name.
I think this is what you are looking for:
public problem()
{
// but how do i refer to method test2 or test3 from here?
zz zObj = new zz();
b bObj = new b();
zObj.test2();
bObj.test3();
}
you first create ZZ
object then you can access everything from that class like below
public problem()
{
ZZ zz = new ZZ();
zz.test2();
zz.a1.test3();
}
There are several problems in code
1.Method should have return type I have not seen in your code
2.Proper Naming conventions has to be followed like class name should start with Capital
letters
Make changes in your code as below
public class Zz{
A a1=new A();
// i can do
a1.b1.test();
public void test2()
{
}
}
//End of class Zz
public class A{
B b1= new B()
public void test3()
{
}
}
//End of class A
public class B{
public void test()
{
}
public void problem()
{
// but how do i refer to method test2 or test3 from here?
Zz zz = new Zz();
//create instance of A
A a=new A();
zz.test2();
a.test3();
}
}
//End of class B
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.