• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Basic  RSS feed

 
Ammy Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I Access Static Field Through Non-static Fields And Vice-versa, If No Then What Is The Reason?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try out? And never ever capitalize each word like that. It's annoying, and makes people skip reading the question completely. That means you won't get any replies.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before asking this question ,if you would try to understand the purpose/meaning of 'static' you would be able to answer it yourself.Its so easy,try it
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members can be accessed from a non static context, but non static members cannot be accessed from a static context.

Its better if you try out things yourself, that way you will learn a lot Know what exactly is static and everything will be clear to you
 
daniel wright
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By placing "static" in your method header, you let the compiler know that it can make that method alive without a method call. Your non static methods aren't alive until they are called. Don't know if that helps you with understanding how that works.
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Why don't you try out?


Because the compiler may be wrong.

The Java language specification rules.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sha Jar wrote:Because the compiler may be wrong.

Sure. But if it is, then what are you going to do? Tell it it's wrong so it will learn and compile your code the next time you try?

If there is a bug in the compiler all you can do is contact the developers (using Sun's bug tracking system if it's Sun's compiler) and hope they release a fix soon. In the mean time you have no other option than a) use a different compiler, or b) work around the compiler error.
 
Steven Mann
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As stated above, understanding the simple logic of how static/non-static methods work will answer your question. Also, I always test ideas/questions regardless of wether the compiler will complain: Thats part of the fun in programming!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members are one per class; instance members may be several per class. If you have static members trying to gain access to instance members, which instance member would they look at?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!