Results 1 to 8 of 8
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default accessing instance variables from static methods

    Please review the code below:

    Java 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.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    The instance variables have to be declared as static. So your carName String would have to be:

    Java Code:
    public static String carName;

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks for the quick response.

    Makes sense but this way my objects of CarClass class won't have their own copy of carName. How can I serve both the purposes?

    Is there a way??

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You could always put the main method in a different class.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class CarClass {
        public String carName;
    
        public static void main(String[] args) {
            // Create instance of enclosing class and save it in
            // a reference, viz, the local variable "carClass".
            CarClass carClass = new CarClass();
            // Use this instance variable to access
            // the member variable "carName".
            carClass.carName = "Mazda";
            //carName = "Mazda";
            // cannot access static member here
        }
    
        public void setterMethod() {
            carName = "Mazda";
            // works fine
        }
    }

  6. #6
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks. It helps.

  7. #7
    Jeff6461 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Issue

    ok so say this code here

    <code>

    //networking code
    public static void main(String args[])
    {
    int serverPort = 3456; // server port number
    double number;
    java.net.ServerSocket sock = null; // original server socket
    java.net.Socket clientSocket = null; // socket created by accept
    java.io.PrintWriter pw = null; // socket output stream
    java.io.BufferedReader br = null; // socket input stream
    int loop=1;



    try
    {
    sock = new java.net.ServerSocket(serverPort); // create socket and bind to port
    System.out.println("waiting for client to connect");
    clientSocket = sock.accept(); // wait for client to connect
    System.out.println("client has connected");
    pw = new java.io.PrintWriter(clientSocket.getOutputStream() ,true);
    br = new java.io.BufferedReader(
    new java.io.InputStreamReader(clientSocket.getInputStr eam()));
    //gets the string
    String msg = br.readLine(); // read msg from client

    do{

    System.out.println("Message from the client >" + msg);
    pw.println("X:"+X ); // send msg to client
    }while(loop==1);
    //pw.close(); // close everything
    //br.close();
    //clientSocket.close();
    //sock.close();
    }

    catch (Throwable e)
    {
    System.out.println("Error " + e.getMessage());
    e.printStackTrace();
    loop=2;
    }


    }

    </code>

    I want to use this code to transfer the X/ Y coords between 2 computers but it will not let me i get a "non static variable X cannot be referenced from a static context" error. If i try to remove the static from the method title it does not run and i get a method error any help here would be great

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Please don't resurrect an old dead thread. I recommend that you repost this in your own thread. If you need to refer to this thread, then why not post a link.
    2) Have a look at the FAQ to see how to use code tags here.
    3) Read the answers to the original question here and you'll see several possible solutions. You also in your new thread show let folks know which line caused the error.

Similar Threads

  1. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  2. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  3. Accessing a static resouces in a web app.
    By sean in forum Threads and Synchronization
    Replies: 3
    Last Post: 08-09-2007, 12:51 AM
  4. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 10:52 AM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •