Campbell Ritchie wrote:But it is probably good style to initialise all instance methods in the constructor. It used to say that in the Java Tutorials. Even if the value is the same as the default values.
Campbell Ritchie wrote:That is not a default constructor. Lots of people say default constructor but you only get a default constructor if you don't write a constructor at all.
You mean some online tutorials get it wrong, toosalvin francis wrote:. . . Some online tutorials too . . .
If a class contains no constructor declarations, then a default constructor is implicitly declared.
Campbell Ritchie wrote:You mean some online tutorials get it wrong, too
Campbell Ritchie wrote:That is not a default constructor. Lots of people say default constructor but you only get a default constructor if you don't write a constructor at all.
code is emotional
salvin francis wrote:
Campbell Ritchie wrote:That is not a default constructor. Lots of people say default constructor but you only get a default constructor if you don't write a constructor at all.
This is actually news to me.
I always thought that any constructor without args was a 'default' constructor.
Some online tutorials too mention it in this manner: http://www.javatpoint.com/constructor
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Tim Holloway wrote:
The "default" constructor is what gets invoked if you create a Java object via the newInstance() method without providing any arguments. It is, not co-incidentally, also the constructor that bean factories - such as the one inside JPA/EJB will invoke for internally-constructed bean instances in the absence of more specialized functions. In fact, EVERY EJB/JPA object must have a no-arguments constructor and yes, the docs call it a default constructor because it must exist or the bean isn't spec-compliant (or functional!) no matter how many other constructors the bean may have or inherit.
Junilu Lacar wrote:
Technically, it's the zero-argument constructor, which is not necessarily a default constructor, which is invoked. If you try calling newInstance() on a class that has only a constructor that does take arguments, you'll get an InstantiationException.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
What's wrong with this sort of code?I presume that Foo() is a misspelling of Foo.Tim Holloway wrote:. . . I also have a few constructors where I have code like so: . . .
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Campbell Ritchie wrote:I would have thought the official name would be in the Java® Language Specification but I couldn't find it. I suspect the official name is “nullary constructor” which I think is a terrible name but I have't been able to confirm that. “Constructor without arguments” will do nicely, thank you
code is emotional
All things are lawful, but not all things are profitable.
Knute Snortum wrote:This
is called a static initializer. It executes when the Class is initialized, before any constructor (which is connected with the instance). So no, they don't have anything to do with the constructor.
code is emotional
code is emotional
Thank youGiovanni Montano wrote:Really cool . . . This forum is really cool . . .