Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Why does Java make my variables become static?

 
webmaster forum
singam  Offline
Activity
0%
 
Regular Coder
Posts: 50
Topics: 19
July 22, 2010, 12:54:13 AM

I'm supposed to get user input of Integers so I don't want it them to be static but it keeps complaining that I have a static reference. C programs don't do this.

I get the same Java Error in at line 21: cannot make static reference to non-static Integer

for this line:
Integer intResult = Integer.parseInt(result); //error: cannot make static reference to non-static Integer


or I tried using the primitive type int:
int intResult = result; //error: cannot make static reference to non-static Integer

but really I need to get it to be an object Integer

and I also tried putting new operator but that gets the same error:
Integer intResult = new Integer.parseInt(result); //error: cannot make static reference to non-static Integer
« Last Edit: March 17, 2011, 10:30:45 PM by Admin »
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
August 01, 2010, 06:10:26 AM

Can you post your entire code? Basically a static variable is one which exists across instances of the object (for instance it exists in memory once, which all object instances access.)

This commonly catches out novice Java programmers with reference to the main (entry) methods of Java which is static (one copy of it exists across the objects.)

What is the type of variable result, post the declaration of it.

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
August 01, 2010, 06:11:14 AM

And no C programs do not, because they are not OO and hence all variables and functions are the equivalent of Java static (unless you take care not to make them so.)

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
New Poster
Posts: 3
Topics: 1
August 12, 2010, 01:56:01 PM

Is "result" an Integer object or an int variable?

Integer. parseInt() takes an Integer object and returns a primitive int.  So
Code:
Integer intResult = Integer.parseInt(result);
should really be:
Code:
int intResult = Integer.parseInt(result)
assuming that "result" is an Integer object.

I guess there are two ways that you can look at this:

1.  Converting from Integer object to int primitive type:
Code:
int intPrimitive = Integer.parseInt(integerObject);

2.  Converting from int primitive type to Integer object:
Code:
Integer integerObject = new Integer(intPrimitive);

I hope this makes sense!
 
webmaster forum
Admin  Offline
*
 
Code Guru
Location: India
Gender: Male
Posts: 1387
Topics: 105
NaviBuster NaviBuster
WWW
August 12, 2010, 09:30:54 PM

@WumpaFruit: Thank you for posting that really make sense.
 
webmaster forum
Activity
0%
 
New Coder
Posts: 17
Topics: 2
August 13, 2010, 08:30:12 AM

C programmers are not recommend, as Java variables and functions are stable and are as good as C scripts of C programs.

immobilien mallorca | Cheap Website Design | chicago movers
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 06, 2012, 08:42:11 AM