In Java, can a static method return an array of objects as its result? -
in java, can static method homecoming array of objects result? i'm pretty sure can homecoming array of objects fine in cases, know objects utilize instance variables non-static. help confusion please.
yes, can. instance variables in other objects not limitation you. limitation static method cannot access instance variables without instance, while have instances homecoming fine.
answer question can tricky, see this:
http://ideone.com/b7uhmt
import java.util.*; class statictest { object[] arr = new object[] { 1, 2, 3 }; static object[] testmethod() { //return arr; // not have instance //return new string[] { "abc", "def" }; // possible statictest o = new statictest(); homecoming o.arr; } public static void main (string[] args) throws java.lang.exception { system.out.println( arrays.aslist( testmethod() ) ); } }
if uncomment return arr;
, there error
error: non-static variable arr cannot referenced static context
but problem different, not case, array cannot returned.
java arrays object static-methods return-type
No comments:
Post a Comment