Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

float number problem

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
whats wrong with this code ?
Code:
public class test
{ 

Float F1 = new Float(0.9F); 
Float F2 = new Float(0.9F); 
Double D1 = new Double (0.9); 

public static void main(String args[])
{

System.out.println(F1.equals(F2)); 

} 

}

why its not compiling ?
 
The error :

"Test.java": Error #: 308 : non-static variable F1 cannot be referenced from a static context at line 13, column 20

Change your code to :
Code:
public class test
{

static Float F1 = new Float(0.9F);
static Float F2 = new Float(0.9F);
Double D1 = new Double (0.9);

public static void main(String args[])
{

System.out.println(F1.equals(F2));

}

}
to make the variable static.

Or to :
Code:
public class test
{

Float F1 = new Float(0.9F);
Float F2 = new Float(0.9F);
Double D1 = new Double (0.9);

public static void main(String args[]) {
  test t = new test();
  t.doIt();
  }

  public void doIt() {
    System.out.println(F1.equals(F2));
  }

}
To make the method call non-static

You should also name your class "Test" with a capital "T" to be in line with the standards.
 
hi, i have questions on your second answer.

Q1
you have created
an object t by :
test t = new test();

what t will acquire now ? i guess, it will acquire instance variables(e.g F1,F2,D1) and doIt method.right ? but does t will include main() also ?

say , i have created 5 objects like t. does it mean there are 5 main() associated with them ?

Q2

you have created t inside the class test itself !! is not it a self reference ? still it works.

Q3

i changed the code a little bit but the code below fails to compile. why?

Code:
public class test
{ 

 Float F1  = new Float(0.9F); 
 Float F2  = new Float(0.9F); 
 Double D1 = new Double (0.9); 


public void method()
{
	test t = new test();

}


public static void main(String args[])
{

t.doIt();

} 


public void doIt()
{
System.out.println(F1.equals(F2)); 	
}



 
The main() method is just an entry point to a class from the command line. It does not actually create an instance of that class. This you must do inside the main method like below ...

Code:
public class Test { 

 Float F1  = new Float(0.9F); 
 Float F2  = new Float(0.9F); 
 Double D1 = new Double (0.9); 



public static void main(String args[]) {
  Test t = new Test();
  t.doIt();

} 


public void doIt() {
 System.out.println(F1.equals(F2));     
}
 
Q1 : Yes, it will acquire instance variables(e.g F1,F2,D1) and doIt method.
main() is not an instance method, but can be called from within t , like
Code:
test.main(new String[] {} );
but that would be recursive (in this situation)

Q1 : If you create 5 objects like t from within main, "there is only 1 main()" , because ... see sedj's answer.

Q2: ... see sedj's answer...
But it looks strange indeed.

Q3 : Your code doesn't compile, because t is not declared nor instanciated when it is used in your main method.

To make it compile and run you COULD change it to :
Code:
public class test
{

Float F1 = new Float(0.9F);
Float F2 = new Float(0.9F);
Double D1 = new Double (0.9);

static test t = null;

public static void method(){
  t = new test();
}

public static void main(String args[]) {
  method();
  t.doIt();
  }

  public void doIt() {
    System.out.println(F1.equals(F2));
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top