View Single Post
  #1 (permalink)  
Old 11-07-2007, 01:41 PM
ravian ravian is offline
Senior Member
 
Join Date: Nov 2007
Posts: 115
Rep Power: 0
ravian is on a distinguished road
Default accessing instance variables from static methods
Please review the code below:

Code:
public class CarClass {

public String carName;

	public static void main(String[] args) {
		carName = "Mazda";
		// cannot access static member here
	}

	public void setterMethod() {
		carName = "Mazda";
                // works fine
	}
}
Main is a static method and I want to access a non static variable (instance variable) called carName in main method. What is the best way of doing that?

Thanks.
Reply With Quote