简体   繁体   中英

why can'y I access a method from within another method

A very basic java question! I'm tying to get my head around how objects interact with each other. I understand that objects are instaniated and their methods can be accessed with .operator. I presumed that when I instantiate an object, it sits on the Heap and any class in that package can access any of the object methods using the (Objectname.methodName) approach.

My problem: when I instantiate the object in main() and try a method directly afterwards, no problem, however, when I instaniate the object in main, but try to access the method from another method outiside main() - I have the problem. In the method setupHello, it gives me an error as not recognising the object hw.

(I using Eclipse for the first time, so it maye be a package problem, but I don't think so.)

Thank you.

public class SayHelloToWorld {  

    public static void main (String [] args){
        SayHelloToWorld sayHello1 = new SayHelloToWorld();

        HelloWorld hw  = new HelloWorld();
        hw.sayHello(); // no problem here

        sayHello1.setupHello();         
    }//end main

    public void setupHello(){       
        hw.sayHello();  // problem here !!!!
    }    
} 

The hw object isn't even in scope when being called. The setupHello method has no way of seeing the hw object.

One option is to declare the object outside of main and make it static.

static HelloWorld hw;  // Declare object here



public static void main (String [] args){

    SayHelloToWorld sayHello1 = new SayHelloToWorld();

    hw  = new HelloWorld();
    hw.sayHello(); // no problem here

    sayHello1.setupHello();


    }//end main

    public void setupHello(){       
        hw.sayHello();  // problem here !!!!
    }

Or edit setupHello to accept a HelloWorld object as a parameter...

public static void main (String [] args){

        SayHelloToWorld sayHello1 = new SayHelloToWorld();

        HelloWorld hw  = new HelloWorld();
        hw.sayHello(); // no problem here

        sayHello1.setupHello(hw);


        }//end main

        public void setupHello(HelloWorld hw){       
            hw.sayHello();  // problem here !!!!
        }

Problem solved!

The object does sit on the heap but only main knows where.
To make setupHello know where it is change public void setupHello(){ to public void setupHello(HelloWorld hw){ and the call to it from sayHello1.setupHello(); to sayHello1.setupHello(hw); .

The object, hw does not exist within the "scope" of the setupHello() method and it was not passed to it.

Add it as a method argument:

public void setupHello(HelloWorld hw)

and pass it to the method:

sayHello1.setupHello(hw);

The only alternative would be to make hw and instance variable, or "field" and then you could access it directly. (Or make it static, but that's definitely not desired here even though it would technically work in the example).

This has to do with the scope of the variable hw. It is local to the main method and thus cannot be accessed from elsewhere.

What you can do is declare the hw variable outside of the main method OR pass it as a parameter to the next method.

You instantiated the hw in the scope of main . Declare it outside of just that method to make it visible to the whole class.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM