Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 04-22-2008, 11:14 PM
Member
 
Join Date: Apr 2008
Posts: 1
wizmang is on a distinguished road
Non-Static method in static context error
Hi, I have a section of code from my program which uses a method inherited from another class.

private void processLabyMenu( KeyEvent event ) {
int keycode = event.getKeyCode();
String x = new String();

if( Prax.Cfg.getString("returnToIntro").equalsIgnoreCa se
(KeyEvent.getKeyText(keycode)) )
{
mainWindow.setMapFile( null );
mainWindow.load();
}


and I get the error
"the non-static method getString(java.lang.String) cannot be referenced from a static context".

what can I put in that "if" statement for it to compile?

Thank you in advance...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 08:01 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
Hei,
Can you post the compleate code?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 05:56 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 183
danielstoner is on a distinguished road
The message is clear: you are calling a a non static method (Prax.Cfg.getString) from a static context. It seems Prax.Cfg is a inner class and you should use an instance of that class in the if condition.
But that being said, a bigger chunk of the code will help with understanding.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 07:08 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Add the keyword 'static' to getString.

eg:
Code:
public static String getString() { }
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 09:51 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Quote:
Originally Posted by danielstoner View Post
The message is clear: you are calling a a non static method (Prax.Cfg.getString) from a static context. It seems Prax.Cfg is a inner class and you should use an instance of that class in the if condition.
But that being said, a bigger chunk of the code will help with understanding.
This is correct you need to create an instance of the class your trying to access. For example

Code:
public class Demo { Prax myPrax; public Demo() { myPrax = new Prax(); myPrax.Cfg.getString(); } }
Think of it this way the:

The way your doing it is calling the actual class. That could have multiple instances of it running in your system. This is fine if your class is static meaning it doesn't change when you create multiple instances of it.

What you want to do is edit a new instance of it.

When you work on your car in real life, are you working on the model car that the manufacture made or are you working on your "instance" of that car.

Last edited by Zosden : 04-24-2008 at 09:55 AM.
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
Explicit static initialization with the static clause Java Tip java.lang 0 04-17-2008 12:07 AM
is overriding static method possible raghu Advanced Java 1 01-22-2008 01:38 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 06:05 AM
Error: non-static variable height cannot be referenced from a static context at line fernando AWT / Swing 1 08-01-2007 10:25 PM
non-static member can not be referenced from a static context christina New To Java 1 07-31-2007 07:11 PM


All times are GMT +3. The time now is 05:43 AM.


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