-3

Okay am just beginning to learn Java and I have a doubt regarding constructors.

isn't constructor just another method with same name as class?? If so Why do we need it? Cant we just use methods?

Puneeth R
  • 95
  • 5
  • 2
    possible duplicate of [Purpose of a constructor in Java?](http://stackoverflow.com/questions/19941825/purpose-of-a-constructor-in-java) – PakkuDon Jul 01 '14 at 12:29
  • Constructor is used to initialize your instance variable . Suppose if create an object then default constructor automatically initialize u value whereas if u create method then u have to call that method every time u create a new object . – sp1rs Jul 01 '14 at 12:29
  • 2
    Isn't faster to just Google? http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html – ThanksForAllTheFish Jul 01 '14 at 12:33
  • A constructor actually makes the object. You can't use any methods (other than static methods) until you have created an object. Are you using any objects yet? (Created using the `new` keyword) – Richard Tingle Jul 01 '14 at 12:46

2 Answers2

2
  1. Constructors have no return type.
  2. They are used for instantiating a class.
  3. They can be called from subclass using Super keyword.
  4. Their name must be same as it's classname
Heaven42
  • 329
  • 1
  • 13
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

Constructors are called on object creation. You can initialize stuff in other methods of course but then you'd have to call them explicitly.

You don't need to write a constructor. But if you want to do things to ready your object, you put in in your constructor.

VonSchnauzer
  • 912
  • 11
  • 17