472,794 Members | 4,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 3610

"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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.