Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 12:41 PM
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-07-2007, 03:11 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
The instance variables have to be declared as static. So your carName String would have to be:

Code:
public static String carName;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 04:18 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
Rep Power: 0
ravian is on a distinguished road
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??
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 06:06 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
You could always put the main method in a different class.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-07-2007, 07:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
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
    }
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-08-2007, 08:53 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
Rep Power: 0
ravian is on a distinguished road
Default
Thanks. It helps.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-01-2009, 09:46 PM
Member
 
Join Date: Mar 2009
Posts: 2
Rep Power: 0
Jeff6461 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-01-2009, 10:09 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static Variables Java Tip java.lang 0 04-16-2008 11:08 PM
Static methods Java Tip Java Tips 0 11-04-2007 05:56 PM
Accessing a static resouces in a web app. sean Threads and Synchronization 3 08-08-2007 11:51 PM
significance of static variables and methods imran_khan New To Java 4 08-02-2007 09:52 AM
Help with static variables bbq Advanced Java 1 06-28-2007 05:38 PM


All times are GMT +2. The time now is 03:55 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org