Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: Mr. Neo on March 19, 2005, 10:54 PM

Title: non-static variable error
Post by: Mr. Neo on March 19, 2005, 10:54 PM

public class First
{
public Second a = new Second();

public static void main(String[] args)
{
System.out.println("First: main");
a.Test();
}
}


How come when I try to call a.Test() I get a "non-static variable a cannot be referenced from a static context" error?  If I change a to being a static variable, everything works out fine.  Does this mean that static variables can only be accessed from within static methods?
Title: Re: non-static variable error
Post by: iago on March 20, 2005, 12:04 AM
Quote from: Mr. Neo on March 19, 2005, 10:54 PM
Does this mean that static variables can only be accessed from within static methods?

No, other way around.  Non-static variables can only be accessed from non-static methods.
Title: Re: non-static variable error
Post by: Mr. Neo on March 20, 2005, 10:00 AM
Thank you for clarifying that.