Results 1 to 1 of 1
-
Calling constructor of same class from a constructor
A constructor of a class can be called from another constructor of the same class. The constructor calling statement (this()) should be the first statement in the constructor. Review the code below.
Java Code:public class ClassA { public ClassA(){ this(2); System.out.println("Constructor without parameter."); } public ClassA(int a){ System.out.println("Constructor with int parameter: " + a); } public static void main(String[] str) { ClassA obj = new ClassA(); } }
Java Code:Constructor with int parameter: 2 Constructor without parameter.
Similar Threads
-
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 10:45 PM -
Constructor calling
By ravian in forum New To JavaReplies: 2Last Post: 12-22-2007, 07:53 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 10:10 AM -
Constructor Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 11-15-2007, 06:44 AM
Bookmarks