Create Java Class with variable(Different AM)

Written by admin on . Posted in Learn By Example

3 Flares Twitter 0 Facebook 1 Google+ 2 Email -- Email to a friend 3 Flares ×

Here we will learn to create Class having variable with different access modifier and their visibility under class’s Visibility.

package com.accessmodifier;

/*
 * Here we will learn to create Class having variable with different access modifier and their visibility under class's
 * Visibility.
 */
public class ClassWithVariableAM {

	/*
	 * In public method we have declared 3 variable with different AM. As public
	 * class can be accessed from any class but it doesn't mean that all
	 * variable will be accessible to class. After Class visibility variable
	 * visibility is checked to see if it is accessible outside class or not
	 */

	// Even class is public private variable will not be accessible to any
	// class(Same or Outside Package)
	private int j;

	public int i;
	protected int k;

}

/*
 * Default / Non Public class can be created in same package and in same Java
 * file. No Access Modifier needs to be mentioned. Class with Default Access
 * Modifier can not be accessed outside package in which it is declared.
 */

class ClassWithoutAM {

	/*
	 * Even variable is public here it can not be accessed outside package in
	 * any condition as Class is not visible outside this package. Variable's
	 * visibility comes only after class visibility.
	 */
	public int i;
}

 

Trackback from your site.

Leave a comment

3 Flares Twitter 0 Facebook 1 Google+ 2 Email -- Email to a friend 3 Flares ×