*
The moose likes Beginning Java and the fly likes logic behind not able to assign a static value to a non static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "logic behind not able to assign a static value to a non static variable" Watch "logic behind not able to assign a static value to a non static variable" New topic
Author

logic behind not able to assign a static value to a non static variable

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121


I dont get the logic behind this
I think it should be possible to assign a value of static variable to a non-static variable .

Understanding is Everything - Peter Lord
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Pratik D mehta wrote:



http://www.lifesbizzare.blogspot.com || OCJP:81%
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Hi,

b is not a static variable while a is thats why you are getting that error
\
Try decalring b in main and see the differnce
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Vishal Hegde wrote:Hi,

b is not a static variable while a is thats why you are getting that error
\
Try decalring b in main and see the differnce


Yes vishal I understand that , if declared in main it will behave as a local variable , but
I just wanted to understand
why we cant assign the nonstatic variable a static value , rather than remembering it .

I assume its just a part of java , maybe there is no logic.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2


Because you don't hold any instance of sample17, you could not access its instance variable.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Raymond Tong wrote:
Because you don't hold any instance of sample17, you could not access its instance variable.


Pratik , i hope your query is resolved, Thats what i meant when i was making it local(Subconciously...)

Thnks Raymond You rock Cheers (FYI: I dont drink beer )
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Thank you vishal and raymond so there is no logic behind - that you cannot give a static variable value to the instance variable directly .

Like for example Change ( the instance variable ) so that from that statement onwards when every new object is created it will get the new value .

It is just the way how java works .



Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Let me use another example with more meaningful names to help you understand.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Pratik D mehta wrote:so there is no logic behind - that you cannot give a static variable value to the instance variable directly .

Of course there's logic behind it--there's no instance variable available to assign a value to.Short version: You have to have an instance before you can access an instance's properties.
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yup thank you david and raymond .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: logic behind not able to assign a static value to a non static variable
 
Similar Threads
Final Funda
STRANGE??????Final variables
instance variables and methods
Primitive Value Assignment
Explain the output