We help IT Professionals succeed at work.

Accessing non public class

prsubject
prsubject asked
on
665 Views
Last Modified: 2012-05-11
In theory if you have all the classes as non-public then you can name the file with whichever name you want to. I have a class with all non-public classes. The class was given a different name. I am not knowing how to access these classes from an other class. I am pasting two classes 1) class with non-public class and 2) public class with main method
//Class 1
package ch1;
public class NonPublic {
	class Test
	{
		public void thistest()
		{
			System.out.println("This is metyhod of first");
			
		}
	}
	
	class MyTest
	{
		public void thatTest()
		{
			System.out.println("This is from method of second class");
		}
	}
}

//Class2
package ch1;

public class Ch1 {
	public static void main(String[] args)
	{
		System.out.println("Hello World");
		NonPublic np = new NonPublic(); //Instantiated the non-public class
                        //How to access classes of NonPublic Class and their methods from here
		
	}
}

Open in new window

Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
and btw they are inner classes, not non-public classes
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
And NonPublic is a public class :)

Commented:
NonPublic is in fact public, as you've started the class declaration with the word 'public'. As you havent specified the inner class accessibility, they have default, which means they can be accessed only be the enclosing class or other classes in the same package. So in your example above you could do this:

NonPublic.MyTest blah = new NonPublic.MyTest()

Open in new window


You could not do this from outsied this package though, unless you declared the inner classes as 'public'
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.