File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class' vs constructor's accessability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class Watch "class New topic
Author

class' vs constructor's accessability

Stanislava Trajlov
Greenhorn

Joined: May 15, 2003
Posts: 15
How do we handle cases when a class and its constructor aren't of the same accessability in JVM 1.4:
1) if the constructor has greater accessability than a class itself:
class AClass{
public AClass(){
}
}
or
class BClass{
protected BClass(){
}
}
2) if the constructor is less accessible than its class:
class CClass{
private CClass(){
}
}
or
public class DClass{
DClass(){
}
}
(there are more than 2 examples of constructor being less accessible than class).
What happens is we create:
- AClass() object from a class located in different package
- BClass() object from a class (other than its subclass) located in different package
- CClass() object from other class in the same package
- DClass() object from a class from different package ?
Do we get a compile error or run-time exception in JVM 1.4?
Is it always the more restricted of the two - class , constructor - that is taken into account?
Thank you!
Stanislava
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
Stanislava
The first thing that you need to look at is class visibility. Then look at method visibility.

- AClass() object from a class located in different package

AClass has a default access modifier so AClass is not visible from outside of that package. So in this case the access modifier on the constructor method does not matter. This will not compile.

- BClass() object from a class (other than its subclass) located in different package

Once again BClass has default access modifier so the class is not visible. This will not compile.

- CClass() object from other class in the same package

This time the class is visible but the constructor is not so this will not compile.

- DClass() object from a class from different package ?

DClass is visible but the constructor is not so this will result in a compile error as well.

Is it always the more restricted of the two - class , constructor - that is taken into account?

You have to look at class visibility first, then the method visibility.
Hope this helps
giang nguyen
Ranch Hand

Joined: May 13, 2003
Posts: 42
If you want to call a constructor of a class to intitialize an object of that class, you must first have access to the class. Once you have access to a class, it then depends on the accessibility to the constructor that decides whether you can call it to initialize the object.
Apply this rule, the compiler will report errors in all cases you mentioned


SCJP 1.4, SCWCD
Stanislava Trajlov
Greenhorn

Joined: May 15, 2003
Posts: 15
It helps, thank you both!
Stanislava
 
 
subject: class' vs constructor's accessability
 
Similar Threads
merging data values of two similar objects
Can anyone explain executing at runtime vs compile?
Class casting/assigning question - REVISED
what linking mechanism uses java (early or late)
Class name's as Identifiers