Jump to content

How to access a variable from a private class in java?

i have a private class called balance.in that class i have a public variable named b1. i need to access this variable from another class.how can i do this?

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Lacrimas said:

these are public classes with private variables. i have a private class with a public variable in it.i need to access that public variable 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Lacrimas said:

Is your top level class private?

yes

Link to comment
Share on other sites

Link to post
Share on other sites

If your private Class is a top level Class, it is impossible to access. Why do you need this Class to be private?

 

If something is private it is only accessible within the Class it is defined. This means that a private Class will only be accessible by the Class that contains it. If the private Class is not contained in another Class, it is impossible to access it.

 

Here's an example of a private inner Class being accessed by an outer Class:

public class MyPublicClass {
	public void MyMethod()
	{
		MyPrivateClass p = new MyPrivateClass();
		System.out.println(p.myPrivateVariable);
	}

	private class MyPrivateClass {
		public int myPrivateVariable = 1;
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×