Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is the first line of a constructor declaration?

 
shoeb sattar
Greenhorn
Posts: 14
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be 2 right? what lead you to an heated discussion?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) is the class declaration.
(2) is the constructor declaration.
(3), (4) and (5) are declarations of methods.
 
shoeb sattar
Greenhorn
Posts: 14
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would be the answer if no constructor was declared explicitly?
 
Yatika Sharma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer would be 2 when no constructor was declared explicitly.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is 2. when no constructor was declared explicitly.
 
shoeb sattar
Greenhorn
Posts: 14
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if there is no constructor declared explicitly, line 2 would not exist
 
shoeb sattar
Greenhorn
Posts: 14
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic