473,320 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3651

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
2
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured...
1
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
0
by: Henry Wu | last post by:
Hi, I am aware that TransparencyKey only works with top-level forms or Non-MDI Child Forms, if one tries to set the opacity or transparencykey property to a MDI-Child form, it will have no effect....
8
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.