• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

private class vs private constructor

 
Ranch Hand
Posts: 18944
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I have a class that requires only the default constructor. I decided I wanted to block any other classes from making any calls to that constructor, so I hard coded the default constructor and made it private.
Then i thought, well if nobody can construct these objects, then their methods are useless... so I might as well make the whole class private. It said....
"modifier private not allowed here"
Assumptions
1.) To make the default constructor private, I must hard code
it and change it to private.
2.) Classes cannot be private? Ever?

Are these correct? I appreciate any input.
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes can have private access, but never your top level class. It must be declared public or default. Constructors can be declared public, protected, private, or default.
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett,

Please read this page and register properly with first name, space, last name. Thanks.

Marilyn
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Josh,
Barry is right, you cannot declare a top-level class private; however, I think the accepted way of making a top-level class private is to declare the default constructor private. This would allow any code within the class to create an instance but prevent any other classes or sub-classes from creating one.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Garrett:
Lets say I have a class that requires only the default constructor. I decided I wanted to block any other classes from making any calls to that constructor, so I hard coded the default constructor and made it private.
Then i thought, well if nobody can construct these objects, then their methods are useless... so I might as well make the whole class private. It said....
"modifier private not allowed here"
Assumptions
1.) To make the default constructor private, I must hard code
it and change it to private.
2.) Classes cannot be private? Ever?

Are these correct? I appreciate any input.



Just a couple of additional notes:
* Having a private constructor does not necessarily mean that no method in the class is unavailable. You can access static methods and variables without creating an instance of the class, assuming those items have appropriate permissions (e.g., they're public).
Here's an example (assume these classes are defined in separate source files):
//source file privConstructor.java
public class privConstructor
{
public static void amethod()
{
System.out.println("Ran amethod()");
}
private privConstructor(){}
}
// source file test.java
public class test
{
public static void main(String args[])
{
privConstructor.amethod();
}
}
* an inner (or nested class) can be private or protected. A top level class can only be declared public or default (no access modifier, also known as friendly, which means accessibly within that package). For more information, you can read http://www.jchq.net/tutorial/01_02Tut.htm
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!