Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2007, 02:41 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 11-07-2007, 05:11 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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, 06:18 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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, 08:06 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You could always put the main method in a different class.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-07-2007, 09:10 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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, 10:53 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks. It helps.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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-17-2008 01:08 AM
Static methods Java Tip Java Tips 0 11-04-2007 07:56 PM
Accessing a static resouces in a web app. sean Threads and Synchronization 3 08-09-2007 01:51 AM
significance of static variables and methods imran_khan New To Java 4 08-02-2007 11:52 AM
Help with static variables bbq Advanced Java 1 06-28-2007 07:38 PM


All times are GMT +3. The time now is 10:22 PM.


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