• 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

Valid places to call a constructor from

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently working with a SCJP study guide and am staring at a rule that I can't seem to agree with.


In addition to the constructor (with no parameters), you can also define nondefault constructors with parameters. However, the constructor name stays the same: the class name. The constructor may be called from inside the class where it is defined or from outside the class, using the following rules:

Outside of the class in which a constructor is defined, the constructor can be called only with the new operator, that is, when you want to create an instance of the class. For example, a code expression new A() in the class B will create an instance of the class A.

Inside the class where a constructor is defined, the constructor can be called from within another constructor, and not from anywhere else.



To me rule #2 means one of two things, Either:

1) A constructor, in order for it to be called from within another constructor, must be called from within the class in which it is defined.

OR

2) If a constructor is called from within the class that defines it, it must be called from within another constructor.

I can write code that violates both interpretations of this rule and compiles fine. Does anybody know what this is supposed to be referring to?

Thanks
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that rule means using a this(); statement? If so, the this() must be the 1st line of the constructor, a bit like a super() in a subclass.
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Campbell I seriously doubt it. I am aware of that rule... I know your suggesting a stretch because your trying to be helpful and I appreciate it.

I think that if you're pulled that far away from the literal meaning by this statement in order to make it fit Java's actual behavior the answer is that the author really mangled his verbiage to the point where its not worth trying to reconstruct and that really helps because I was starting to think that I was either missing very important basic Java concepts or that my internal English parser was failing. Neither of which is a happy thought.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule is loosely and badly written, but not totally inaccurate.

A default constructor is actually added to the class when the compiler encounters no written constructor. It is identical to what you would get if you wrote thisonly in a non-public class the "public" would be omitted.

You can write no-argument constructors which do nothing, in which case they are indistinguishable from default constructors (I am not sure whether you call them default), but they might have private or protected access instead.

You can get no-argument constructors which do something, in which case I wouldn't call them default. Example

You can get constructors with arguments.

You can call any constructor which is accessible. Inside a class, you may need a reference to an instance of the same class; a Node in a LinkedList is a good exampleAs you can see, the addNumberAtEndOfList method calls the class constructor with the new operator.

I could have written the constructor differentlyYou are familiar with the super() call in a subclass. Well, what that does is to call the constructor in the superclass with that parameter list. The this() call calls the constructor in the same class with that parameter list.

Every constructor has super(); or this(); as its first line, never both. If you don't write them for yourself, the compiler will impute them to your constructors and add super(); as the first line.

The nearest I can think to what that article means is the bit about super() or this() as the first line. You can't say this() anywhere but the first line of a constructor.

This really is a bad recommendation for that book, isn't it? Did you find more such errors?
 
reply
    Bookmark Topic Watch Topic
  • New Topic