Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

about constructor

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the rule for the access specifier for the constructor?
eg if class has public specifier then does this imply that all the construtors of the class are also public.And if yes can we change it?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no.. the class scope is different and the constructor scope is different.. even if u have a public class.. u can name ur constructor private..
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Access Specifier has nothing to do with Constructor Access Specifier.

Example :


In the above example access specifier for class Temp is public but it's constructor is private. Even by default constructor will get the default specifier (which doesn't depends on class's specifier)
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey if u have a class with default access and if u dont provide any constructor, then the automatically created constructor has the same accees as the class, i.e., default. but if u write ur own constructor, u can write it wid any other access specifier also.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check All Possible Combinations of Features and Modifiers.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)If you explicitly define any constructor for the class then the accessability modifier of the defined constructor(or constructors) is completely independent of the accessability modifier of the class.

2)If you want the compiler to generate a constructor for you(ie you are not defining even a single constructor)then the accessability modifier of the generated constructor is dependent on the accessability modifier of the class.

3)Access mode of the generated constructor is public for public classes, and default for classes with any other access mode.

Hope It is clear.
Let me know If I am wrong.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Access mode of the generated constructor is public for public classes, and default for classes with any other access mode.



Actually according to the Java Language Specification, the access modifier of the default constructor is the same as the access modifier of the class.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,

Class Temp1 will not be able to create an object of class Temp, as the constructor of Temp is private. In fact, class Temp1 should not be able to even "see" a constructor of Temp, since it is marked private.

Only the members of Temp would now able to create an instance of Temp.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith,

I think we have to Check out for inner classes.

Bcoz inner classes can have all(public, protected, default/package, private)access modifiers.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Girish Nagaraj:
Hi Keith,

I think we have to Check out for inner classes.

Bcoz inner classes can have all(public, protected, default/package, private)access modifiers.



This is the link to what the Java Language Specification says.

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi keith,

I checked out for inner classes with diff. modifiers.
What you said was right.
I have modified my points accordingly, Let me know if their is any mistake.

1)If you explicitly define any constructor for the class then the accessability modifier of the defined constructor(or constructors) is completely independent of the accessability modifier of the class.

2)If you want the compiler to generate a constructor for you(ie you are not defining even a single constructor)then the accessability modifier of the generated constructor is dependent on the accessability modifier of the class.

3)The access modifier of the generated constructor is the same as the access modifier of the class.

Hope It is clear.
Let me know If I am wrong.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic