This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static method init() cannot be referenced from a static context

 
LaTeef Lusk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help. I am getting this error when compiling. Here is my code. I am basically trying to use main to call my init function.
Can someone please point me in the right direction. Thanks in advance!

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Temperature extends java.applet.Applet
{
public static void main(String[] args) {

init();

}

private TextField fahrenheitInput;
private TextField celsiusInput;
private Button convert;

public void init()
{
setLayout(new BorderLayout());

convert = new Button ("Convert My Temperature");
fahrenheitInput = new TextField();
celsiusInput = new TextField();

Panel p;

p = new Panel();
p.setLayout(new GridLayout(0, 2));

p.add(new Label("Enter the temperature in Fahrenheit:"));
p.add(fahrenheitInput);

p.add(convert);

add("North", p);

p.add(new Label("Here is the converted temperature in Celsius:"));
p.add(celsiusInput);

add("Center", p);
}

public boolean action (Event evt)
{
// the event target once the button is clicked
if (evt.target == convert)
{
// Convert the temp and assign it to the celsius textfield
int f = Integer.parseInt(fahrenheitInput.getText());
int c = 5 * (f-32) / 9;
String newCelsius = Integer.toString(c);
celsiusInput.setText(newCelsius);

// Event handled
return true;
}
else
return false;
}

}




 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method is a static method, whenever you access any non-static methods or variables from a static method then either those methods or variables should be static or can be accessed through instance of the class. Only static methods and variables could be accessed directly.

for Example in your case:

Replace this line with init(); in main method
new Temperature().init();

Or

Modify your init method as:

public static void init(){
------------
------------
}


 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I have noticed that it is an Applet class and you can't test Applet the way you are testing. You have to create a HTML file like:



and then open this html in browser. Hopefully it should work.

Good Luck.....
 
LaTeef Lusk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile the class now this happens.

C:\IBM51\WebSphere\AppServer\java\bin>javac c:\Temperature.java
c:\Temperature.java:17: init() in Temperature cannot override init() in java.applet.Applet; overriding method is static
public static void init()
^
c:\Temperature.java:19: non-static method setLayout(java.awt.LayoutManager) cannot be referenced from a static context
setLayout(new BorderLayout());
^
c:\Temperature.java:21: non-static variable convert cannot be referenced from a static context
convert = new Button ("Convert My Temperature");
^
c:\Temperature.java:22: non-static variable fahrenheitInput cannot be referenced from a static context
fahrenheitInput = new TextField();
^
c:\Temperature.java:23: non-static variable celsiusInput cannot be referenced from a static context
celsiusInput = new TextField();
^
c:\Temperature.java:31: non-static variable fahrenheitInput cannot be referenced from a static context
p.add(fahrenheitInput);
^
c:\Temperature.java:33: non-static variable convert cannot be referenced from a static context
p.add(convert);
^
c:\Temperature.java:35: non-static method add(java.lang.String,java.awt.Component) cannot be referenced from a static context
add("North", p);
^
c:\Temperature.java:38: non-static variable celsiusInput cannot be referenced from a static context
p.add(celsiusInput);
^
c:\Temperature.java:40: non-static method add(java.lang.String,java.awt.Component) cannot be referenced from a static context
add("Center", p);
^
10 errors

C:\IBM51\WebSphere\AppServer\java\bin>
 
LaTeef Lusk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem. Your solution was correct! Thank you so much!
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Max Lock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone knows whats wrong with this code?
I allways get error: "non-static method prime(int) cannot be referenced from a static context"

 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't reopen old threads with a new question; you ought to have started a new thread for that.
 
Max Lock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the error is the same, so I thought that opening a new thread would be inappropriate.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why wasn't the answer in this topic sufficient? We can't elaborate the answer, if you don't elaborate why your question is different, or what you don't understand?

Henry
 
Max Lock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this error "non-static method init() cannot be referenced from a static context",
on this line: if( primes(i) ).
primes(int k) is a boolean function, IF looks for an boolean expresion as its argument, so i dont get whats wrong with this code.
I havent coded in java before (this is my first time), but this code would run on c/c++ without a hitch.
I would like you to tell me whats wrong and how to make it right, so I dont make any more mistakes like that.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with booleans. That error message is quite clear. You cannot access an instance (non-static) member of the class from a static context. You will have to come out of the static context, then you can gain access to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic