Results 1 to 8 of 8
- 11-07-2007, 12:41 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
accessing instance variables from static methods
Please review the code below:
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?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 } }
Thanks.
- 11-07-2007, 03:11 PM #2
The instance variables have to be declared as static. So your carName String would have to be:
Java Code:public static String carName;
- 11-07-2007, 04:18 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
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??
- 11-07-2007, 06:06 PM #4
You could always put the main method in a different class.
- 11-07-2007, 07:10 PM #5
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 } }
- 11-08-2007, 08:53 AM #6
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Thanks. It helps.
- 03-01-2009, 09:46 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
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
-
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
-
Using Static Variables
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:08 PM -
Static methods
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:56 PM -
Accessing a static resouces in a web app.
By sean in forum Threads and SynchronizationReplies: 3Last Post: 08-08-2007, 11:51 PM -
significance of static variables and methods
By imran_khan in forum New To JavaReplies: 4Last Post: 08-02-2007, 09:52 AM -
Help with static variables
By bbq in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 05:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks