aspose file tools*
The moose likes Beginning Java and the fly likes non-static method init() cannot be referenced from a static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static method init() cannot be referenced from a static context" Watch "non-static method init() cannot be referenced from a static context" New topic
Author

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

LaTeef Lusk
Greenhorn

Joined: Mar 28, 2009
Posts: 15
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

Joined: Apr 04, 2009
Posts: 93
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(){
------------
------------
}



Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
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

Joined: Mar 28, 2009
Posts: 15
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

Joined: Mar 28, 2009
Posts: 15
I found the problem. Your solution was correct! Thank you so much!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Max Lock
Greenhorn

Joined: Nov 15, 2009
Posts: 3
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please don't reopen old threads with a new question; you ought to have started a new thread for that.
Max Lock
Greenhorn

Joined: Nov 15, 2009
Posts: 3
Well the error is the same, so I thought that opening a new thread would be inappropriate.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Max Lock
Greenhorn

Joined: Nov 15, 2009
Posts: 3
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
 
Don't get me started about those stupid light bulbs.
 
subject: non-static method init() cannot be referenced from a static context