简体   繁体   中英

When calling a method from another class, do variables in that method have to be instantiated in the class calling the method?

Another similar question: When calling a method (from separate class) that returns a value which is derived from another class (from within the separate class). Do we need to instantiate the variables of derived class in that calling method?

If you are calling a method in another class, the variables already are instantiated. In order to call said method, you must have either a) constructed a new object ie

Object myObject = new Object();
myObject.myMethod();

or b) the method is static, which means it is instantiated already and you can call it as

Object.myMethod();

in either case, the variables used by the class are instantiated upon construction, and those used locally by the function are allocated once the function is called.

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