0

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();

}

}
1

4 Answers 4

0

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();

}

Comments

0

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();
}

1 Comment

It doesn't but i don't see any limitation why it should not be. It doesn't access any object variables, so it can be called in static context.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.