• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

static initialiser blocks?

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm just wondering how come this code works? I understand that static initialiser blocks are used to initialise static variables when the class is constructed so I'm guessing I am creating a static map for the class Locations and initialising that map but how come when calling the methods in the main method I don't need to declare the object as static ?

and how does this code actually work? I know what it does and what it's doing but I want to now whats happening with the static intitialser blocks and also if I create a second instance of the same class with will the second instance share the first instances static variables such as the map??

thanks
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adam Chalkley wrote:Hi I'm just wondering how come this code works? I understand that static initialiser blocks are used to initialise static variables when the class is constructed so I'm guessing I am creating a static map for the class Locations and initialising that map but how come when calling the methods in the main method I don't need to declare the object as static ?

and how does this code actually work? I know what it does and what it's doing but I want to now whats happening with the static intitialser blocks and also if I create a second instance of the same class with will the second instance share the first instances static variables such as the map??



Static variables are located with the class and *not* with any instance. If you create zero instances of the class (but the class has been loaded), there is one copy of the static variables. If you create a million instances of the class, there is still one copy of the static variables.

As for declaring an object as static, that doesn't really make sense does it?

Henry
 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's tempExits? Why do you have it at all? As far as I can see it goes out of scope when the initialiser block finishes and disappears into cyber‑limbo never to be seen again.
Why have you got a static reference to a Map (not a static Map, please)? Why did you mark ti static when the rest of the class has instance members? Are you trying to create a wrapper class aroudn a Map?
When the class is loaded, all its static fields are initialised to their default values (=null for non‑final fields) unless the declaration has a = sign in. In which case that = sign is executed as well.
Then the static initialisers are executed. Beware of multiple initialisers; unlike constructors which are called by outside code, initialisers run in a certain order: the order they are written in. To avoid problems with such ordering, write one single initialiser. All the code in the initialiser is executed. As Henry says, that code is only run once and there is only one instance of your Map. Even if there are 1,000,000 instances of the class. Or even if there are no instances.
As for invoking methods on the Locations instance in the main  method (line 13): that instance is a local variable in the main method and you are not allowed the keyword static inside a method (unless inside a local class).
 
Adam Chalkley
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies guys,much appreciated,

the reason I used tempExits was to make my class Location class immutable so I can pass tempExits in the contsructor it would probably been better if I would have posted the Location class aswell which I will now,

 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, that is confusing code with repeatedly assigning tempExits to a new Map.

There are better ways to make a Map immutable:-
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic