General Java Questions I
+ super(...) or this(...) as first instructor (often implicit)
everything else
I very much do not like trying to lump the two concepts together, especially in
introductory courses. Conceptually they are very, very different things.
A constructor is a unique element (even if there are several). It has the name of the
class, its declaration is different, and it doesn t have the same syntax as a method. It
plays a unique role. You can call a method from a constructor, but you cannot call a
constructor from a method.
I say constructors are distinct from methods, and for students, blurring the distinction
will lead to problems.
by Chuck McCorvey, Chris Wolfe, Paul Lutus
Q: Simple question: why constructor doesn t work in following example?
class Start {
public void Start() {
System.out.println("Konstruktor START");
}
}
public class Test {
public static void main(String[] args) {
Start s = new Start();
}
}
Answer: Because you have included the return type void in the method declaration,
it becomes a normal method, that just happens to have the same name as the class
so it won t get used as a constructor. Remove the void and it should work.
Vince Bowdren
P.S. by John: If you do not specifically define any constructors, the compiler inserts
an invisible zero parameter constructor "behind the scenes". Often this is of only
theoretical importance, but the important qualification is that you only get a default
zero parameter constructor if you do not create any of your own.
Your program used this zero parameter constructor and you saw nothing...
Q: Why we can not declare constructor as final ?
Answer: The keyword final when dealing with methods means the method cannot be
overridden.
Because constructors are never inherited and so will never have the oportunity to be
overridden, final would have no meaning to a constructor.
Q: In Java, does exist a function like sprintf in C ?
Answer:
http://www.efd.lth.se/~d93hb/java/printf/index.html
a free Java version of
fprintf(), printf() and sprintf() hb.format package
Q: If I declare an array of an objects, say Dogs, is that memory taken when I
create the array or when I create the objects in the aray when I declare this array:
file:///F|/a_jsite/350_tips/general_java I.htm (5 of 33) [2001 07 08 11:24:51]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved