Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Accessing non public class

Posted on 2011-04-20
4
Medium Priority
?
595 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

0
Comment
Question by:prsubject
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35431054


            NonPublic.MyTest mt = np.new MyTest();
                mt.thatTest();
0
 
LVL 92

Expert Comment

by:objects
ID: 35431065
and btw they are inner classes, not non-public classes
0
 
LVL 92

Expert Comment

by:objects
ID: 35431098
And NonPublic is a public class :)
0
 
LVL 8

Expert Comment

by:colr__
ID: 35431251
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'
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question