Home » Java programming language

Can we access private variable outside of the class in Java?

In this article, we are going to learn about the property of private variable in java. We will learn can we access it outside of the class in Java?
Submitted by Preeti Jain, on March 21, 2018

Private variable in Java

Private variable can declare with the private keyword.

In the below example, we will see we can access private variable in the same class.

Example:

class CanPrivateVariableAccessInTheSameClass{

	/* Declare private variable named x */
	private int x;

	/* Define constructor for private 
	variable initialization */
	CanPrivateVariableAccessInTheSameClass(int x){
		this.x = x;
	}

	/* Define method to print value of variable x */
	public void privateVariableAccess(){
		System.out.println("value of x is :" +x);
	}

	/* Main method definition */
	public static void main(String[] args)
	{
		/* create CanPrivateVariableAccessInTheSameClass 
		object and pass the value of private variable */
		CanPrivateVariableAccessInTheSameClass cpvaitsc = new CanPrivateVariableAccessInTheSameClass(10);
		/* call CanPrivateVariableAccessInTheSameClass method 
		with the help of CanPrivateVariableAccessInTheSameClass 
		object */
		cpvaitsc.privateVariableAccess(); 
	}
}

Output

D:\Java Articles>java CanPrivateVariableAccessInTheSameClass
value of x is :10

In the below example, we will see we cannot access private variable outside the class.

Example:

class CanPrivateVariableAccessInTheDifferentClass{
	/* Declare private variable named x */
	private int x=10;

	/* Define method to print value of variable x */
	public void privateVariableAccess(){
		System.out.println("value of x is :" +x);
	}

	public static void main(String[] args){
	}
}

class OutsideClass extends CanPrivateVariableAccessInTheDifferentClass{
	public static void main(String[] args){
		CanPrivateVariableAccessInTheDifferentClass cpvaitdc = new CanPrivateVariableAccessInTheDifferentClass();
		/* x is variable of CanPrivateVariableAccessInTheDifferentClass class 
		so we can call x variable with the help of 
		CanPrivateVariableAccessInTheDifferentClass object */
		System.out.println(cpvaitdc.x);
	}
}

Output

D:\Java Articles>javac CanPrivateVariableAccessInTheDifferentClass.java
CanPrivateVariableAccessInTheDifferentClass.java:27: error: x has private access
 in CanPrivateVariableAccessInTheDifferentClass
System.out.println(cpvaitdc.x);
                           ^
1 error





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.