• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Static

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between static block and static method?
which one is better?
please explain it with a piece of code.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by static block?

Do you mean a static initialiser? eg.

static {
// Do something.
// Do something else.
}

If so, this is like a contructor but is called when the class is loaded rather then when the object is contructed.

If I have misunderstood, I apologise.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manoj kumar jena:
which one is better?


They are different things with different purposes, so asking which is better is a meaningless question.
 
Matthew Plant
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would use a static initialiser to set up static variables before you access them via your static methods.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a bit like asking "what is the difference between a hammer and a hammerhead shark? Which one is better?"
 
Marshal
Posts: 79984
398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch, Manoj.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To fully understand this, let's look at the sequence of events. Consider the following simple class.


Now suppose we are in another class. Supppse this line



is the first instance of a Minter object that is declared. At that time, the Java class loader will load the code for the Minter class. First, all of the static stuff is seen to. In an area of memory shared by all instances of the class, go the static instance variables and methods. This includes the constructors. The static block is executed. All this occurs before the birth of the dirst object.

Next, the first instance of the Minter class is created. It gets assigned the first ID. Each new instance of a Minter gets an ID one higher than its predecessor.

I hope this simple example helps.

JMM
 
I once met a man from Nantucket. He had a tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic