A Public Vs. Nonpublic Classes

Probably we used to declare the method and class as a public .
But the there is also the classes where there is no any public
or any private .
We can make two or many classes in a single class file.
* But when declaring numerous class the class file must contain
at least one public class whose class must match to class file
name.

A simple class containing two class with it’s package :
[sourcecode=’java’]
//package
//Test1.java
package Test1;
public class Test1{…}
class Test2{…}[/sourcecode]
Another class containing with two class with pacakage:
[sourcecode=’java’] //package
// X.java

package X;
public class X{….}
class Y{….}[/sourcecode]
At this time we can’t instantiate any variable from X.java to Test1.java Because it’s been blocked by a wall of Package.
A structure diagram of above file will looks like this:

public3

We can make a link between these files:
By importing its package from one class to another.
[sourcecode=’java’]//Test1.java
//package
import X.*;

package Test1;
public class Test1{…}
class Test2{…}[/sourcecode]
Now the structure looks like this:
public21

The above code doesn’t care about the non public class Y of X.java because of it’s non public feature. The public class Test1.java can’t make a object even if we import the package X.

Leave a Reply