• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this a method, constructor or something else?  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am discussing this with my brother, but haven't come to a final answer yet. In the attached picture a class StaticSuper has been defined. In this class are:

1) static{System.out.println("super static block");

2) StaticSuper { System.out.println("super constructor");

Questions:

1) Are these both constructors?
2) Is one a method and one a constructor?
3) Could someone explain me how to recognize a method and a constructor in Java?
Page.PNG
[Thumbnail for Page.PNG]
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. 1) is a static initializer, 2) is a constructor, because it has no declared return type and its name equals to that of the class.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Static initialization blocks: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first block of code is a static initializer.  The second block is a constructor.  Constructors have the same name as the class and have no return type.  That is, not void, no return type at all.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not compile because second one need () to become a constructor like below
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't actually compile, does it?
The constructor is missing its ().
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

That is correct. Thanks for pointing that out.

Cheers
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your questions have been answered but I would just add a couple of points:

1. The static initializer (sometimes called static block) only runs on the creation of the classes first instance i.e. like all static "stuff" it is a class level entity. Therefore if you had this code:

where Class1 has a static initializer, the code within the initializer runs only on the creation of c1.

2. You can have multiple constructors. Each one would need to have different params signature
3. All static blocks in the inheritance tree run before any constructors run
4. You can have non-static initializer blocks which run on each instance creation

If any of this is wrong I'm sure someone will correct me :-)

All the best,

PaulC.

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.

I found this very helpful, talking about the execution of static blocks, static initialization blocks and constructors:

static block -> Initialization block -> and finally Constructor.

static block -> This static block will be get executed only once when the control come to the class.(JVM Load this class)

Initialization block -> This Initialization block will be get executed whenever a new object Created for the Class (It will be executed from second statement of the Constructor then following constructor statements- remember First statement of the Constructor will be Super()/this())

Constructor -> This will be get whenever a new object is created.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small program which shows when the static init block and non-static init blocks run. Also shows how the constructors fire:

See if you can work our what should be displayed on execution.
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it:

1 In Class Level Static Block
2 In Instance Level Static Block
3 In 2 arg Con
4 In Instance Level Static Block
5 In 1 arg Con
6 In Instance Level Static Block
7 In 0 arg Con

let me know if I am right .
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run the program and see!
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:Is it:

1 In Class Level Static Block
2 In Instance Level Static Block
3 In 2 arg Con
4 In Instance Level Static Block
5 In 1 arg Con
6 In Instance Level Static Block
7 In 0 arg Con

let me know if I am right .
Nearly. However, you've missed something from in here:
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The this statement invokes on the public Employee(String n, double s) constructor and therefore runs the System.out.println("In 2 arg Con");.

So the final answer is (after I runned the program):
1 In Class Level Static Block
2 In Instance Level Static Block
3 In 2 arg Con
4 In Instance Level Static Block
5 In 2 arg Con
6 In 1 arg Con
7 In Instance Level Static Block
8 In 0 arg Con

Cheers bro for the exercise, much appreciated.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:The this statement invokes on the public Employee(String n, double s) constructor and therefore runs the System.out.println("In 2 arg Con")
Correct i.e. the first thing the one arg constructor does is run the two arg constructor :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!