aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Constructors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final Constructors?" Watch "Final Constructors?" New topic
Author

Final Constructors?

Shiny
Ranch Hand

Joined: May 29, 2000
Posts: 45
I took Jxam & Jaworski tests which contradict the usage of modifier 'final' for Constructors. Can we declare a constructor as final? Since they are not inherited, it should not matter in an implicit sense.
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Hi Shiny
Following is from JLS:
The access modifiers public, protected, and private are discussed in �6.6. A compile-time error occurs if the same modifier appears more than once in a constructor declaration, or if a constructor declaration has more than one of the access modifiers public, protected, and private.
Unlike methods, a constructor cannot be abstract, static, final, native, or synchronized. A constructor is not inherited, so there is no need to declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java Virtual Machine to verify that superclass constructors are always properly invoked during object creation.
Thanks
ARS Kumar.


ARS Kumar, Sun Certified Programmer for Java 2 Platform
http://www.automatedsqa.com/
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
The reasoning is probably `they can't be inherited, so modifiers other than visibility modifiers are not allowed.'
The one line test:
public class ConstructorTest { final ConstructorTest() {} }
Found 1 semantic error compiling "ConstructorTest.java":
1. public class ConstructorTest { final ConstructorTest() {} }
<--->
*** Error: final is not a valid constructor modifier.
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Hi friends
I have compiled some points about constructors. Please add if you think that anything else can be added to the list or correct is anything is wrong.
1. The valid modifiers of constructors are public,protected and private. So they can't declare with any of these modifiers ( final, abstract, static,synchronized or native )
2. Constructors are not inherited.
3. If you don't have a constructor declared then the compiler will insert a non argument constructor.
4. A constructor can throws execeptions.
5. Calling both super() and this() in one constructor will generate a compilation error.
6. Coding a return with out any expression ( return; ) is valid in constructors. [b] i am about this ! I am not able to visualize the senario when this will be needed. [b]
7. A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have a constructor that takes no arguments.
8. You can't instantiated a class with private constructor.[b] i am about this too ! I am able to understand the reason behind this but not able to visualize the senario when this will be needed. [b]
Any help appreciated.
Thanks
ARS Kumar.


[This message has been edited by ARS Kumar (edited June 06, 2000).]
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Hi Kumar,
Please go through the previous discussion on this topic. http://www.javaranch.com/ubb/Forum24/HTML/000239.html
Hope this helps !!
Regards,
Milind
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Hi Milind
The link you provided doesn't talk about the following
6. Coding a return with out any expression ( return; ) is valid in constructors. i am about this ! I am not able to visualize the senario when this will be needed.
8. You can't instantiated a class with private constructor. i am about this too ! I am able to understand the reason behind this but not able to visualize the senario when this will be needed.
Any help appreciated.
Thanks
ARS Kumar.

[This message has been edited by ARS Kumar (edited June 07, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
ARS Kumar,
Perhaps you would want to add the following to your point no. 5
'If super() or this() is added, it should always be the first statement in the constructor'
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Kumar,
I just wrote 2 very small progs to visualize your above 2 points. Please go through them. Basically constructors are NOT methods and there can't be any return value inside the constructor code like return 1 OR return "some name" etc. But you can use just 'return' statement if you want to break your logic in bet and return from the constructor.
Private ctors are used , in order NOT to make an instance of this class FROM ANY OUTSIDER classes. For example you write a stub like class with all static members ALL STATIC VAR/ ALL STATIC mrthods, and you just use them like ClassName.theMember format. The java.lang.Math class is one such type. Remember ? We use all Math functions just like that.. Math.sin(..) Math.cos(...) Math.min(..) etc. You can't make an instance of this Math class since its ctors are private and not available to outside world. Does this make sense now ?
regds
maha anna
<pre>

class Test {
String name = "Java";
public Test() {}
public Test(String name) {
if(name == null)
return;
//Do further processing
this.name = name;
}
public static void main(String[] args) {
}
}
class AllStatic {
public static int var1;
public static short var2;
public static String var3;
public static void method1() {}
public static void method2() {}
public static void method3() {}
public static void method4() {}
}


</pre>
ARS Kumar
Ranch Hand

Joined: May 22, 2000
Posts: 108
Thanks maha anna for helping me to figure this out and to you too Sandra for adding another point to list
ARS Kumar
Shiny
Ranch Hand

Joined: May 29, 2000
Posts: 45
Thanks all you Guys/Gals for, you have increased the probability of my passing SCJP2 by atleast 5%.
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Further to Maha's reply,
Private ctors are used , in order NOT to make an instance of this class FROM ANY OUTSIDER classes.
Thus, instances of such classes can still be created. This has to be done without using a constructor, probably using a public, static method. This method can give a fine-tuned control over the instance creation process
The example below controls the instance creation process using the createInstance method so that no more than one object of type Test2 is active at any time.
Some situations may need this type of control.
Feel free to correct me.
Regards
RajSim

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Constructors?