• 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

Final Constructors?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all you Guys/Gals for, you have increased the probability of my passing SCJP2 by atleast 5%.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic