Access Specifiers:
Access Specifiers are the keyword which are used to control the access. There are
four types of access specifiers:
a) public b) private c) protected d) default
a) private:
1) It apply to global variables, static variables, methods, constructors and inner class.
2)Outer class can not be private.
3)It can be accessed within class only, not outside class because scope is very
limited.
4)Local variable can’t be private.
b) default:
1) It apply to global variables, static variables, methods, constructors, inner class and
outer class.
2)It can be accessed only within a same package.
3)Whenever access specifier is not provided it will be treated as default. It doesn’t
require any keyword
c)protected:
1) It apply to global variables, static variables, methods, constructors and inner class.
2) It can’t be applied to local variable.
3) It can be accessed within the same package and in different package if
inheritance exists while calling
d)public:
1) It applies to outer class, inner class, global variables, static variables, methods
and constructors
2) It can be accessed from same class or different class in same package or different
package
3) Local variables can’t be public because they have a limited scope, within the
method/loop/block in which they are declared.
Note:
- Local variable can only be final
- The only modifiers to outer class are: public, default, final, abstract, strictfp. If
you use any other modifier it will be an error.
- The least accessible modifier is private.
- The most accessible modifier is public.
- Recommended modifier for variables is private and recommended modifiers
for method is public.
- Decreasing scope order:
Public>protected>default>private
Static Keyword :
- If the value of a variable is not varied from object to object such type of
variables is not recommended to declare as instance variables. We have to
declare such type of variables at class level by using static modifier.
- In the case of instance variables for every object a separate copy will be
created but in the case of static variables for entire class only one copy will be
created and shared by every object of that class.
- Static variables will be created at the time of class loading and destroyed at
the time of class unloading hence the scope of the static variable is exactly
same as the scope of the. Class file. The static members were a part of
PermGen before Java8. However, since Java 8, they're apart of heap
memory.
- Static variables should be declared within the class directly but out side of any
method or block or constructor.
- Static variables can be accessed from both instance and static areas directly.
- We can access static variables either by class name or by object reference
but usage of class name is recommended.
- But within the same class it is not required to use class name we can access
directly.
- Static variables also known as class level variables or fields.
Example:
class Test
{
intx=100;
static int y=200;
public static void main(String[]args)
{
Test t1=new Test();
t1.x=888;
t1.y=999;
Test t2=new Test();
System.out.println(t2.x+"----"+t2.y); //100-------999
}
}
How to access static variable:
There are two ways to access the static variables.
1. By using class name
2. By using object name
Program for static variables:
class Statickeyword
{
public static void main(String args[])
{
Statickeyword obj = new Statickeyword();
static int a=5;
System.out.println(Statickeyword.a); //by using class name
System.out.println(obj.a); //by using object name
}
}
Output:
5
5
In this example, we are calling static variable by using static keyword class name,
also calling it by using object obj as shown in above program.
Why it is called as single copy storage?
Because for multiple object only one copy of variable will be made for a given static
variable.
package com.wipro;
public class Test10
{
int a=5;
static int b=5;
public static void main(String args[])
{
Test10 sd2=new Test10();
System.out.println("non static>>"+sd2.a++);
System.out.println("static>>"+sd2.b++);
Test10 sd3=new Test10();
System.out.println("non static>>"+sd3.a++);
System.out.println("static>>"+sd3.b++);
Test10 sd4=new Test10();
System.out.println("non static>>"+sd4.a++);
System.out.println("static>>"+sd4.b++);
Test10 sd5=new Test10();
System.out.println("non static>>"+sd5.a++);
System.out.println("static>>"+sd5.b++);
}
}
Output:
non static>>5
static>>5
non static>>5
static>>6
non static>>5
static>>7
non static>>5
static>>8
Note – We cannot call non-static member from static member because static
variables stored into memory before object creation and non-static variables stored
into memory after object creation.