Skip to content
March 6, 2009 / Narayan

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 :

 //Test1.java
 //package

  	package Test1;
   	public class Test1{...}
   	class Test2{...}

Another class containing with two class with pacakage:

 //package
 // X.java

	package X;
	public class X{....}
	class Y{....}

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.

 //Test1.java
 //package
 	import X.*;

  	package Test1;
   	public class Test1{...}
   	class Test2{...}

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: