Results 1 to 5 of 5
Thread: Quick Question!!!!!!!!
- 05-07-2012, 03:34 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Quick Question!!!!!!!!
public class Question1{
private List myList;
public static void main (String[] args){
myList = newLinkedList();
myList.add("one");
myList.add("two");
System.out.println(myList);
}
}
Could someone please explain why this doesn't compile whilst this does:
public class Question1{
private List myList;
public void execute(){
myList = newLinkedList();
myList.add("one");
myList.add("two");
System.out.println(myList);
}
public static void main (String[] args){
new Question1().execute():
}
}
Thank you
- 05-07-2012, 03:44 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Quick Question!!!!!!!!
What errors do you get?
- 05-07-2012, 03:49 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: Quick Question!!!!!!!!
the first one doesn't compile because 'private list myList;' is not static. I don't understand why this works for the second one though
- 05-07-2012, 03:52 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Quick Question!!!!!!!!
In the first one you are directly referencing an instance variable through main. Since this is an instance variable, it only exists in an instance of the object in which it belongs. In the second approach you create an object and then refer to it's integers with the 'new' line. In approach 1, change the variable to
Java Code:private static List myList;
- 05-07-2012, 11:46 AM #5
Re: Quick Question!!!!!!!!
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Quick question
By Qsc in forum New To JavaReplies: 6Last Post: 07-25-2011, 04:36 AM -
Really quick question
By shadycharacter in forum New To JavaReplies: 2Last Post: 04-22-2010, 10:06 PM -
Quick question
By sAntA199 in forum New To JavaReplies: 2Last Post: 12-09-2009, 03:01 AM -
One last quick question
By jigglywiggly in forum New To JavaReplies: 7Last Post: 01-26-2009, 08:53 AM -
Quick Question
By Spenc in forum New To JavaReplies: 3Last Post: 09-22-2008, 02:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks