468,170 Members | 2,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

Non-Static VS Statice error message

Hello Java NG,
I not sure if this is the right NG for this type of question but if not
please let me know which is, TIA

Any way first off let me say I'm a student and this WAS last weeks lab,
turned in, graded and passed so I'm not trying to get someone to do my lab
assignments, but after I got this back I was reading about the DecimalFormat
and I tried to format my output but I keep getting an error message. can
anyone please tell me what I'm doing wrong
Thanks Jim

Error Message=========================================== ======
C:\Lab3\Variables.java:47: non-static variable Fmt cannot be referenced from
a static context
System.out.println( NumInFeet +" is converted to " +
Fmt.format(NumInMeters) + " Meters" );
^
1 error

Tool completed with exit code 1
End Error Message=========================================== ==

Below is the code

import java.io.*; // needed for BufferedReader, InputStreamReader, etc.
import java.text.DecimalFormat; // needed for the DecimalFormat method

//Part A
************************************************** **************************
***
public class Variables
{
//Declare a new instance of DecimalFormat
public DecimalFormat Fmt = new DecimalFormat("0.###");

// Create a BufferedReader for keyboard input
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );

public static void main ( String [] args ) throws IOException
{

//Declare variables, note the use of doubles here and float in
part B
int NumInFeet;
double NumInMeters;
final double FEETTOMETERS = 0.3048;

// Prompt the user
System.out.print( "Enter a measurement in feet to convert to
meters: " );

// Read a line of text from the user.
String input = stdin.readLine();

//convert the input to integer value
NumInFeet = Integer.parseInt( input ); // converts a String
into an int value

//perform the math comversion on the user input
NumInMeters = NumInFeet * FEETTOMETERS;

// Display the answer back to the user.
System.out.println( NumInFeet +" is converted to " +
Fmt.format(NumInMeters) + " Meters" );

}
}
Jul 17 '05 #1
1 3338

"James" <tv******@ix.netcom.com> wrote in message
news:3F*****************@newsread3.news.pas.earthl ink.net...
Hello Java NG,
I not sure if this is the right NG for this type of question but if not
please let me know which is, TIA

Any way first off let me say I'm a student and this WAS last weeks lab,
turned in, graded and passed so I'm not trying to get someone to do my lab
assignments, but after I got this back I was reading about the DecimalFormat and I tried to format my output but I keep getting an error message. can
anyone please tell me what I'm doing wrong
Thanks Jim

Error Message=========================================== ======
C:\Lab3\Variables.java:47: non-static variable Fmt cannot be referenced from a static context
System.out.println( NumInFeet +" is converted to " +
Fmt.format(NumInMeters) + " Meters" );
^
1 error

Tool completed with exit code 1
End Error Message=========================================== ==

Below is the code

import java.io.*; // needed for BufferedReader, InputStreamReader, etc. import java.text.DecimalFormat; // needed for the DecimalFormat method

//Part A
************************************************** ************************** ***
public class Variables
{
//Declare a new instance of DecimalFormat
public DecimalFormat Fmt = new DecimalFormat("0.###");

// Create a BufferedReader for keyboard input
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );

public static void main ( String [] args ) throws IOException
{

//Declare variables, note the use of doubles here and float in
part B
int NumInFeet;
double NumInMeters;
final double FEETTOMETERS = 0.3048;

// Prompt the user
System.out.print( "Enter a measurement in feet to convert to
meters: " );

// Read a line of text from the user.
String input = stdin.readLine();

//convert the input to integer value
NumInFeet = Integer.parseInt( input ); // converts a String
into an int value

//perform the math comversion on the user input
NumInMeters = NumInFeet * FEETTOMETERS;

// Display the answer back to the user.
System.out.println( NumInFeet +" is converted to " +
Fmt.format(NumInMeters) + " Meters" );

}
}

You declare Fmt as a member of class Variables. It's not static, which means
it's associated with a specific instance of the Variables class. Each
instance you create has its own copy of this variable, and without creating
any instances, which you don't, this variable essentially doesn't exist. As
it must belong to some particular object you must refer to it by its
relation to an object. Your main method is static. It has to be. This means
that it doesn't need an instance of the class in order to exist, and any
time it is run, it's running without any reference to whatever objects of
this class you may have created. To use a non-static variable or method from
a static context (finally coming to your error message), you must either a)
first create an instance of the class that contains whatever you want to use
or b) make the non-static thing static. So in your example:
....
NumInMeters = NumInFeet * FEETTOMETERS;
Variables variables = new Variables();
System.out.println(NumInFeet + " is converted to "
+ variables.Fmt.format(NumInMeters) + " Meters");
....

or add static to the declaration of Fmt:
public static DecimalFormat Fmt = new DecimalFormat("0.###");

Apart from that, your code conventions leave a lot to be desired. Is that
really how they're teaching you? Have a look here for the proper way to
write code:
http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html

Also, please post to comp.lang.java.help instead of here in the future.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mario | last post: by
2 posts views Thread by Mark Stijnman | last post: by
25 posts views Thread by Yves Glodt | last post: by
reply views Thread by Christopher Attard | last post: by
8 posts views Thread by John Hazen | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.