| Author |
Which is the first line of a constructor declaration?
|
shoeb sattar
Greenhorn
Joined: Feb 05, 2010
Posts: 9
|
|
I came across this question in Khalid Mogul's book, its seems simple but sparked a heated argument among my friends.can some one throw some light on the issue. the exact question was:
Which is the first line of a constructor declaration in the following code?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
|
It should be 2 right? what lead you to an heated discussion?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
(1) is the class declaration.
(2) is the constructor declaration.
(3), (4) and (5) are declarations of methods.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
shoeb sattar
Greenhorn
Joined: Feb 05, 2010
Posts: 9
|
|
some one said when class is declared at that time default constructor is declared,
so line 1 is answer...can you please explain
there is confusion between when a default constructor is declared?
we also know that...
If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
shoeb sattar wrote:
If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor
As you quoted, since the class already declares a constructor, the compiler will not provide a no-arg constructor...
|
 |
shoeb sattar
Greenhorn
Joined: Feb 05, 2010
Posts: 9
|
|
|
what would be the answer if no constructor was declared explicitly?
|
 |
Yatika Sharma
Greenhorn
Joined: Aug 26, 2011
Posts: 18
|
|
|
The answer would be 2 when no constructor was declared explicitly.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3862
|
|
shoeb sattar wrote:what would be the answer if no constructor was declared explicitly?
Well, in that case there'd be an implicit declaration of a no-arg constructor, but it would be misleading to say that any particular line declared it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
shoeb sattar wrote:some one said when class is declared at that time default constructor is declared . . .
I think that was at best confusing. You get a constructor in the class when you write one. The compiler creates a default constructor, so:-
If you do not write a constructor, there is no constructor until the compiler runs and creates it.
You can see default constructors like this
javac Foo.java
javap -c Foo
|
 |
Yatika Sharma
Greenhorn
Joined: Aug 26, 2011
Posts: 18
|
|
|
the answer is 2. when no constructor was declared explicitly.
|
 |
shoeb sattar
Greenhorn
Joined: Feb 05, 2010
Posts: 9
|
|
if there is no constructor declared explicitly, line 2 would not exist
|
 |
shoeb sattar
Greenhorn
Joined: Feb 05, 2010
Posts: 9
|
|
Matthew Brown wrote:
shoeb sattar wrote:what would be the answer if no constructor was declared explicitly?
Well, in that case there'd be an implicit declaration of a no-arg constructor, but it would be misleading to say that any particular line declared it.
Well, I think you are right and closest to what I can understand..
I don't think such question would be asked in scjp exams?
|
 |
 |
|
|
subject: Which is the first line of a constructor declaration?
|
|
|