Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 .
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote:

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because you don't hold any instance of sample17, you could not access its instance variable.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me use another example with more meaningful names to help you understand.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup thank you david and raymond .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic