non-static method cannot be referenced from a static context

  • Thread starter Johannes Beekhuizen
  • Start date
J

Johannes Beekhuizen

Hello,

What does it mean and how can I solve it? When I try to compile the
application
"TaalKeuze" I get this error:
non-static method
setDefaultLocales(java.lang.String,java.lang.String,java.lang.String) cannot
be
referenced from a static context
The sources for TaalKeuze and the class Language are at the bottom of this
message.
I am very new at programming in Java, so it may be something trivial. But even
walking is difficult when you just start :)

Regards,

Hans.

jdh dot beekhuizen at duinheks dot xs4all dot nl

=== import ===
import javax.swing.*; import java.awt.*; import java.awt.event.*;

import nl.jaybee.*;

public class TaalKeuze extends JPanel {
JPanel languagePanel;
String selectedLanguage;
private JFrame controllingFrame;
public TaalKeuze(JFrame f) {
controllingFrame = f;
languagePanel = createLanguagePanel();
add(languagePanel);
}
private JPanel createLanguagePanel() {
selectedLanguage = Language.currentLanguageName;
JPanel p = new JPanel();
JComboBox languageCombo = new JComboBox(Language.languageList);
languageCombo.setEditable(false);
languageCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel languageLabel = new JLabel("Taal: ");
languageLabel.setLabelFor(languageLabel);
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(languageLabel);
p.add(languageCombo);
p.setAlignmentX(Component.LEFT_ALIGNMENT);
return p;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Relotec taalkeuze");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final TaalKeuze newContentPane = new TaalKeuze(frame);
newContentPane.setOpaque(true);
newContentPane.setLayout(new BoxLayout(newContentPane,
BoxLayout.Y_AXIS));
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
String language;
String country;
if (args.length == 2) {
language = new String(args[0]);
country = new String(args[1]);
}
else {
try {
String envLang = System.getenv("LANG");
language = envLang.substring(0,2);
country = envLang.substring(3,5);
}
catch (NullPointerException e) {
language = null;
country = null;
}
}
Language.setDefaultLocales (language, country, "TaalKeuze");
System.out.println(language);
System.out.println(country);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
=== tropmi ===

=== import ===
package nl.jaybee;
import java.util.*;
public class Language {
public String defaultLanguage,
defaultCountry;
public String currentLanguage,
currentCountry;
public Locale defaultLocale,
currentLocale;
public String currentLanguageName;
public ResourceBundle messageBundle;
public static final String[] languageList = {
"Nederlands",
"English",
"Deutsch",
"Français" };
public static final String[] languageCodes = {
"nl",
"en",
"de",
"fr" };
public static final String[] countryCodes = {
"NL",
"EN",
"DE",
"FR" };
public void setDefaultLocales (String inputLanguage,
String inputCountry,
String inputMessages ) {
if (inputLanguage == null ) {
defaultLanguage = languageCodes[0];
}
else {
defaultLanguage = inputLanguage;
}
if (inputCountry == null ) {
defaultCountry = countryCodes[0];;
}
else {
defaultCountry = inputCountry;
}
currentLanguage = defaultLanguage;
currentCountry = defaultCountry;
defaultLocale = new Locale(defaultLanguage, defaultCountry);
currentLocale = new Locale(defaultLanguage, defaultCountry);
setLanguageName ();
messageBundle =
ResourceBundle.getBundle(inputMessages, currentLocale);
}
public void setLanguageName () {
currentLanguageName = "unknown";
for (int ii = 0; ii < languageCodes.length; ii++) {
if ( currentLanguage == languageCodes[ii] ) {
currentLanguageName = languageList[ii];
}
}
}
}
=== tropmi ===
 
J

jan V

What does it mean and how can I solve it?

The logic of a static method can not call a non-static method of the same
class. That's a rule in the language. When you'll understand more about
object-orientation, you'll see why that makes perfect sense. You need to be
able to see the fundamental difference between static and non-static
methods... I suggest you read some (good) introductory book(s) on Java. e.g.
Thinking in Java (free).
 
D

dave

Hello,

What does it mean and how can I solve it? When I try to compile the
application
"TaalKeuze" I get this error:
non-static method
setDefaultLocales(java.lang.String,java.lang.String,java.lang.String) cannot
be
referenced from a static context
The sources for TaalKeuze and the class Language are at the bottom of this
message.
I am very new at programming in Java, so it may be something trivial. But even
walking is difficult when you just start :)

Regards,

Hans.

jdh dot beekhuizen at duinheks dot xs4all dot nl

=== import ===
import javax.swing.*; import java.awt.*; import java.awt.event.*;

import nl.jaybee.*;

public class TaalKeuze extends JPanel {
JPanel languagePanel;
String selectedLanguage;
private JFrame controllingFrame;
public TaalKeuze(JFrame f) {
controllingFrame = f;
languagePanel = createLanguagePanel();
add(languagePanel);
}
private JPanel createLanguagePanel() {
selectedLanguage = Language.currentLanguageName;
JPanel p = new JPanel();
JComboBox languageCombo = new JComboBox(Language.languageList);
languageCombo.setEditable(false);
languageCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel languageLabel = new JLabel("Taal: ");
languageLabel.setLabelFor(languageLabel);
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(languageLabel);
p.add(languageCombo);
p.setAlignmentX(Component.LEFT_ALIGNMENT);
return p;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Relotec taalkeuze");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final TaalKeuze newContentPane = new TaalKeuze(frame);
newContentPane.setOpaque(true);
newContentPane.setLayout(new BoxLayout(newContentPane,
BoxLayout.Y_AXIS));
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
String language;
String country;
if (args.length == 2) {
language = new String(args[0]);
country = new String(args[1]);
}
else {
try {
String envLang = System.getenv("LANG");
language = envLang.substring(0,2);
country = envLang.substring(3,5);
}
catch (NullPointerException e) {
language = null;
country = null;
}
}
Language.setDefaultLocales (language, country, "TaalKeuze");
System.out.println(language);
System.out.println(country);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
=== tropmi ===

=== import ===
package nl.jaybee;
import java.util.*;
public class Language {
public String defaultLanguage,
defaultCountry;
public String currentLanguage,
currentCountry;
public Locale defaultLocale,
currentLocale;
public String currentLanguageName;
public ResourceBundle messageBundle;
public static final String[] languageList = {
"Nederlands",
"English",
"Deutsch",
"Français" };
public static final String[] languageCodes = {
"nl",
"en",
"de",
"fr" };
public static final String[] countryCodes = {
"NL",
"EN",
"DE",
"FR" };
public void setDefaultLocales (String inputLanguage,
String inputCountry,
String inputMessages ) {
if (inputLanguage == null ) {
defaultLanguage = languageCodes[0];
}
else {
defaultLanguage = inputLanguage;
}
if (inputCountry == null ) {
defaultCountry = countryCodes[0];;
}
else {
defaultCountry = inputCountry;
}
currentLanguage = defaultLanguage;
currentCountry = defaultCountry;
defaultLocale = new Locale(defaultLanguage, defaultCountry);
currentLocale = new Locale(defaultLanguage, defaultCountry);
setLanguageName ();
messageBundle =
ResourceBundle.getBundle(inputMessages, currentLocale);
}
public void setLanguageName () {
currentLanguageName = "unknown";
for (int ii = 0; ii < languageCodes.length; ii++) {
if ( currentLanguage == languageCodes[ii] ) {
currentLanguageName = languageList[ii];
}
}
}
}
=== tropmi ===


Je maakt een dubbele fout :

Ten eerste probeer je de niet-static method 'setDefaultLocales' aan te roepen
vanuit de static method 'main'. Static en niet-static gaan niet goed samen.

(Als vuistregel moet je 'static' zoveel mogelijk vermijden, behalve als
dat echt niet anders kan. In jouw geval kan dat wel, zie mijn aanpassingen
onderaan).

Ten tweede probeer je method 'setDefaultLocales' aan te roepen alsof
het een static method is. Static methods kun je namelijk aanroepen
met clasnaam.methodnaam, dus zoals jij deed : Language.setDefaultLocales

Maar method 'setDefaultLocales' is niet static en moet dus via een
class-instance aangreopen worden, bv :

Language lang = new Language();
lang.setDefaultLocales (language, country, "TaalKeuze");

---

Ok, zoals gezegd kun je static methods het beste vermijden omdat
het er anders op uitdraait dat je variabelen en andere methods
waarmee zo'n static method mee in aanraking komt, ook static
moet gaan maken en voor je het weet, is je hele applicatie static ! :)

Hieronder heb ik je code aangepast en de static methods gewijzigd
in 'gewone' methods. (Alleen 'main' is nog static, want dat kan niet
anders).


import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class TaalKeuze extends JPanel {
JPanel languagePanel;
String selectedLanguage;
private JFrame controllingFrame;
public TaalKeuze(JFrame f) {
controllingFrame = f;
languagePanel = createLanguagePanel();
add(languagePanel);
}
private JPanel createLanguagePanel() {

//selectedLanguage = Language.currentLanguageName;

JPanel p = new JPanel();
JComboBox languageCombo = new JComboBox(Language.languageList);
languageCombo.setEditable(false);
languageCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel languageLabel = new JLabel("Taal: ");
languageLabel.setLabelFor(languageLabel);
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(languageLabel);
p.add(languageCombo);
p.setAlignmentX(Component.LEFT_ALIGNMENT);
return p;
}
private void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Relotec taalkeuze");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final TaalKeuze newContentPane = new TaalKeuze(frame);
newContentPane.setOpaque(true);
newContentPane.setLayout(new BoxLayout(newContentPane,BoxLayout.Y_AXIS));
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
TaalKeuze tk = new TaalKeuze(args);
}

public TaalKeuze(String[] args) // Constructor.
{
String language;
String country;
if (args.length == 2) {
language = new String(args[0]);
country = new String(args[1]);
}
else {
try {
// String envLang = System.getenv("LANG"); // Wordt niet meer ondersteund.

String envLang = "engeng";
language = envLang.substring(0,2);
country = envLang.substring(3,5);
}
catch (NullPointerException e) {
language = null;
country = null;
}
}

Language lang = new Language();
lang.setDefaultLocales (language, country, "TaalKeuze");
System.out.println(language);
System.out.println(country);
}
}


import java.util.*;
public class Language {
public String defaultLanguage,
defaultCountry;
public String currentLanguage,
currentCountry;
public Locale defaultLocale,
currentLocale;
public String currentLanguageName;
public ResourceBundle messageBundle;
public final String[] languageList = {
"Nederlands",
"English",
"Deutsch",
"Français" };
public final String[] languageCodes = {
"nl",
"en",
"de",
"fr" };
public final String[] countryCodes = {
"NL",
"EN",
"DE",
"FR" };
public void setDefaultLocales (String inputLanguage,
String inputCountry,
String inputMessages ) {
if (inputLanguage == null ) {
defaultLanguage = languageCodes[0];
}
else {
defaultLanguage = inputLanguage;
}
if (inputCountry == null ) {
defaultCountry = countryCodes[0];;
}
else {
defaultCountry = inputCountry;
}
currentLanguage = defaultLanguage;
currentCountry = defaultCountry;
defaultLocale = new Locale(defaultLanguage, defaultCountry);
currentLocale = new Locale(defaultLanguage, defaultCountry);
setLanguageName ();
messageBundle =
ResourceBundle.getBundle(inputMessages, currentLocale);
}
public void setLanguageName () {
currentLanguageName = "unknown";
for (int ii = 0; ii < languageCodes.length; ii++) {
if ( currentLanguage == languageCodes[ii] ) {
currentLanguageName = languageList[ii];
}
}
}
}
 
J

jan V

Ten tweede probeer je method 'setDefaultLocales' aan te roepen alsof
het een static method is. Static methods kun je namelijk aanroepen
met clasnaam.methodnaam, dus zoals jij deed : Language.setDefaultLocales

This is an English language newsgroup, if you don't mind too much... please
post in English, or don't post at all.
 
D

dave

This is an English language newsgroup, if you don't mind too much... please
post in English, or don't post at all.

What are YOU complaining about ? As someone
from Belgium, you should be able to read it ! :)

But ok, point taken.
 
D

David Segall

Hello,

What does it mean and how can I solve it?
I found the following explanation from "Jacob" to a similar question
of mine helpful:

To call a non-static method (myMethod) you need
an instance of the class to access it through:

instance.myMethod(); or
this.myMethod();

The latter case is when you're within an instance
already (i.e. within a non-static method of the
same class) and as "this." is implied, you normally
just write "myMethod()".

In your case you are within a static method (where
there is no "this") so you cannot call just
"myMethod()", i.e. "this.myMethod()".

You have three options:

o Create an instance and call instance.myMethod()
o Make myMethod static
o Make the method you call it from non-static

The problem you refer to is common when dealing
with main() which is static:

public class HelloWorld
{
public void printHello()
{
System.out.println ("Hello World");
}

public static void main (String args[])
{
printHello(); // Error

// Correct:
HelloWorld helloWorld = new HelloWorld();
helloWorld.printHello();
}

}
 
J

jan V

Ten tweede probeer je method 'setDefaultLocales' aan te roepen alsof
What are YOU complaining about ? As someone from Belgium, you should be
able to read it ! :)

Maybe I can, maybe I can't ;-)

Anyway, on the topic of human language use, I also strongly advocate the use
of English for all identifier names plus comments when programming (whether
in Java or in any other programming language which uses English keywords).

There are a number of good reasons for this:

a) if you really value your programs (i.e. if you're on the "intellectual
property" wavelength), then your programs better use the de facto language
of business and IT: English (otherwise you may one day be confronted with an
eager buyer for your IP, but when your source code is browsed the
prospective buyer may say "Hang on a sec, what kind of source code is this?
Our programmers can't make head nor tail of this gibberish... Sorry, the
deal is off.")

b) you can post extracts to newsgroups like c.l.j.p and everyone will be
able to understand the bulk of the extract
 
A

Andrew Thompson

...
Anyway, on the topic of human language use, I also strongly advocate the use
of English for all identifier names plus comments when programming (whether
in Java or in any other programming language which uses English keywords).
[ reasons trimmed ]

Thank you. I had been wanting to say that for a long time,
But as someone who speaks *only* English, it felt ..wrong,
or perhaps selfish*.

* ..well that, and I was afraid people would point and laugh.
 
J

jan V

This is an English language newsgroup, if you don't mind too much...
please post in English, or don't post at all. ..
Anyway, on the topic of human language use, I also strongly advocate the use
of English for all identifier names plus comments when programming (whether
in Java or in any other programming language which uses English
keywords).
[ reasons trimmed ]

Thank you. I had been wanting to say that for a long time,

My pleasure.
* ..well that, and I was afraid people would point and laugh.

Andrew Thompson.. afraid? Nah, don't believe it ;-)
 
D

dave

..
Anyway, on the topic of human language use, I also strongly advocate the use
of English for all identifier names plus comments when programming (whether
in Java or in any other programming language which uses English keywords).
[ reasons trimmed ]

Thank you. I had been wanting to say that for a long time,
But as someone who speaks *only* English, it felt ..wrong,
or perhaps selfish*.

* ..well that, and I was afraid people would point and laugh.

Hey Andrew, ever considered to become a school-teacher
or a cop ? I think that would really suit you !

Imagine how much fun that would be ! Then you could
tell people how they *should* behave ALL DAY and get
even paid for it ! Much better then be volunteer-cop
in a newsgroup. Waddaya think ?
 
A

Andrew Thompson

On Fri, 05 Aug 2005 14:03:38 GMT, jan V wrote:

[English as an *only* language]
Andrew Thompson.. afraid? Nah, don't believe it ;-)

D'Oh! I forgot to add the wink!

So, belatedly..

P.S. ;-)
 
D

dave

Hey Andrew, ever considered to become a school-teacher
Be honest. You're still just p*ssed about this, eh?
<http://groups.google.com.au/group/comp.lang.java.programmer/msg/2878f8482a2f9be0>

It is not *my* fault you do not know the first thing about
web deployment in the real world (wide web), yet choose to
provide advice on it!

Nope, that's not it.

It is that about 30% of all messages in this newsgroup
are messages from you telling people that they are
not showing the *right behaviour*.

This newsgroup can do without your annoying presence
just fine. Please get a life. Or a job as a real-life cop.
 
J

John B. Matthews

[...]
It is that about 30% of all messages in this newsgroup
are messages from you telling people that they are
not showing the *right behaviour*.

In a recent sample, Andrew Thompson posted less than 8% of the messages
on this group, and only a fraction of them dealt with behavior.
This newsgroup can do without your annoying presence just fine.

I disagree.
Please get a life. Or a job as a real-life cop.

Or you could simply expand your kill file by an entry or two: one for
him and one for me:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,183
Members
46,719
Latest member
login dogas.info

Latest Threads

Top