This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is a class's static members?  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this everything hat is in the class? class variables..methods etc? or is it a sub class within a class?

thanks
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static member of any class is a property of class rather than an object i.e., it is common to all the objects.
unlike non static members,objects don't have distinct copy of these members.they are used when you actually
want to define some kind of functionality in a class that will remain equal for all objects of the class in which it
is declared.for ex-suppose you need to count every time a instance of a class is instantiated,you can achieve it
via this by declaring a static variable and increment it each time in a constructor when it is instantiated.they are
also used to define a constant by using a final keyword along with it.static members are accessed via class name
rather than an object.

you can have a static -->
1.variable
2.static nested class
3.static methods
4.static block-they are actually used generally for changing default values of a static variable and are executed when
class will load in JVM.
please see this link for static blocks.
and this one,oracle tutorial,for other static members.

Hope this will help!

kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i have tried to explain it pictorially.i have used the words "i can talk to other objects" it means if one do some change in it then it will be visible to others.

Thankyou.

kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . .static block-they are actually used generally for changing default values of a static variable and are executed when class will load in JVM. . . .
I think you mean static initialisers. They are not easy to understand, and they can cause problems if there are multiple initialisers because multiple initialisers run in a certain order whereas fields are not ordered. They are used for initialising fields, and a single static initialiser is sometimes the best way to initialise your static fields. You can read about them in the Java® Language Specification (=JLS). That part of the JLS is quite easy to understand, unlike this part which tells you that static initialsiers are not members of the class.
 
arshi Bano
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can characterize class individuals static utilizing static catchphrase. When we pronounce an individual from a class as static it implies regardless of what number of objects of the class are made, there is one and only duplicate of the static part.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi arshi,welcome to ranch!
arshi Bano wrote:We can characterize class individuals static utilizing static catchphrase. When we pronounce an individual from a class as static it implies regardless of what number of objects of the class are made, there is one and only duplicate of the static part.
i don't get you?
can you please elaborate your words(in the last post).
Campbell Ritchie wrote:I think you mean static initialisers.

yeah i mean the same,"static initializers".
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that there is only ever one of each static member of a class, irrespective of how many or how few instances there are.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!