• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Call a method from a constructor

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !
Can I make a method call from a constructor as given below :




thanks
-Sara
 
Marshal
Posts: 79040
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
Constructors are there to set up the initial state of your object. So you can in theory call any method from a constructor method, but you really ought only to call methods which set up your object's initial state.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, that should be fine.

Do note that "this" is not assured to be good for anything until the constructor is done. Be careful not to pass "this" from the constructor to anything that needs a complete ready-to-use object.
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Stan !
 
Campbell Ritchie
Marshal
Posts: 79040
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sara,
U can certainly do that. But take care of following situation.

class Base
{

Base()
{
disp();
}
public void disp()
{
System.out.println("in disp of Base");
}

}

class Derived extends Base
{

Derived()
{

}
public void disp()
{
System.out.println("in disp of Derived");
}

}

class OverrideDemo
{
public static void main(String[] args)
{

Base b1 = new Base(); // case 1 : Base class disp called
Base b2 = new Derived(); // case 2: Derived class disp called

}
};

If there is a call to a method from constructor in Base class & if u create an object of type Base, it works fine as in case 1. But if you derive a class from such a base class & you override that method in the derived class. The derived class method will be called from Base class constructor.


Warm Regards,
meghana
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


private String getPwd(String car_type) throws Exception



This is just bad practise. Do something like


private String getPwd(String car_type) throws PasswordNotFoundException



instead.

where PasswordNotFoundException extends the Exception class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic