• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Is it possible to invoke a method before even the object gets created?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the program, i want to understand

class First{
First(){
System.out.println("In first constructor");
func();
}
public void func(){
System.out.println("In First func");
}
}
public class TestSubClass extends First{
TestSubClass(){
System.out.println("In Test SubClass constructor");
func();
}
public void func(){
System.out.println("In TestSubClass func");
}

public static void main(String args[]){
TestSubClass tsc = new TestSubClass();
}

}



The out put is

In first constructor
In TestSubClass func
In Test SubClass constructor
In TestSubClass func


Why is the second line of output "In TestSubClass func", but not "In First func".
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the method is overridden. It is calling the func() method of the actual object, even though its constructor has not yet been called. This is why it's usually a bad idea to call methods that can be overridden in the constructor; the instance fields of the subclass have not yet been initialized so if you attempt to use them in that method you will get strange behaviour. Consider:
The initialization of x to 13 occurs after the Super constructor has finished, so after the call to method(). That's why it's still 0 inside that call.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice and precise explanation by Rob . Quality tools like PMD also report that such behavior can throw NullPointerException.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
beacuse, this func method has been override. and " tsc " is parent class.
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

DELETED wrote:


You have already been asked to change your display name here and here.

Please edit your name to use a real first and last name as your current name violates Javaranch's Naming Policy. See the Naming Policy for more details.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it be useful to make all constructor called methods private, or
would this create design problems with subclasses and later changes?

Jim... ...
 
Rob Spoor
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In an ideal world they would be private and/or final, but of course this world is not ideal. Sun's API is full of calls to overridable methods inside constructors (e.g. several of JTable's constructors call setModel which is public and non-final).
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
Jim... ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic