Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Wednesday, 4 November 2015

Access and Non-Access Modifiers in java


Modifiers are keywords that you add to those definitions to change their meanings.

There are two types of modifiers in java: access modifiers and non-access modifiers. The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.

There are 4 types of java access modifiers:
   
 1. private
 2. default
 3. protected
 4. public

1.  private: 


private modifiers access only within class. If we try access private modifiers outside of class ,it produce compilation error. We There is two class user and test.User class have private variable and methods. If we try access private data out side of class then it gives error.

package com.javamodifier;

class User {
     private String name ="java";
      private void display(){
          System.out.println(" name of user is ="+ name);
    }
 }


public class Test{
public static void main(String[] args) {
            User user = new User();
            System.out.println( user.name); //compilation error   
            user.display() ////compilation error
        }
 }


How we can access private data outside class?

For this see public modifiers . Public modifies explain below in this post.


2 Default :


 Default has scope only inside the same package. If we try access default modifiers outside of class , it gives error.

There is two classes Demo and User in different packages.

package com.javamodifier;
import com.javatest.User;


public class Demo {

public static void main(String[] args) {
           User user = new User();
           System.out.println(user.name);// CE
     }
}



package com.javatest;

public class User {

         String name ;
         public void display(){
         System.out.println("name of user ="+name);
    }
}



3. protected : 


Protected has scope within the package and all sub classes. In this Example We access Student's protected method by inheritance.

package com.javamodifier;

import com.javatest.Student;

public class Demo extends Student {
public static void main(String[] args) {
           Student student = new Student();
           student.print();
     }
}


package com.javatest;

public class Student {
          protected void print(){
               System.out.println(" Student Called");
         }
}


Output:

Studnet Called

4. Public:


Public scope is visible everywhere. In previous examples we can access print method of student class without inherintance ,if it declare as public.


package com.javamodifier;

import com.javatest.Student;

public class Demo {
        public static void main(String[] args) {
            Student student = new Student();
              student.print();
        }
}


package com.javatest;

public class Student {
      public void print(){
           System.out.println(" Student Called");
       }
}






How to Access Private Modifiers Outside Class:

We can access private modifiers outside class using public method of this class(withoout using inherintance). Below we give example. Emp class have private data mamber empId and private method diplaySalary. These private modifiers we access throw public accessEmp() method.



package com.javamodifier;

public class MainTest {
public static void main(String[] args) {
Emp emp = new Emp();
emp.AccessEmp();
}
}

class Emp{
private int empId=10;
private void displaySalary(){
System.out.println("salary is = 10000");
}

public void AccessEmp(){
System.out.println("Emp id is ="+empId);
displaySalary();
}

}



Output:

Emp id is =10
salary is = 10000


Non Access Modifiers:


There is some non-access modifiers of java.

The static modifier for creating class methods and variables

The final modifier for finalizing the implementations of classes, methods, and variables.

The abstract modifier for creating abstract classes and methods.

The synchronized and volatile modifiers, which are used for threads.






Monday, 2 November 2015

Java - Variable Types With Example

Variable is a name of reversed memory area of any values. In java values of variable can be change during execution of program.
In java there is three type variable

1. Local variable
2. instance variable
3. static variable

1. Local Variable :


i. Variable declare inside method is called local variable.
ii. These variable only access inside the specific method/block/constructor.
ii. Access modifiers cannot be used for local variables.

public class LocalVariable {
public static void main(String[] args) {
        LocalVariable localVariable = new LocalVariable();
       localVariable.display();
 }

     public void display(){
             int i = 10;// Local Varaible
             System.out.println(" i ="+i);
       }
}

Output:
 i =10

2. Instance variable :


i. Variable declare inside the class but outside method is called instance variable .
ii. Instance variable belongs to object , not to class.
Iii. Access modifiers can be used for instance variables.

public class InstanceVariable {
int i =20;
public static void main(String[] args) {
InstanceVariable instanceVariable = new InstanceVariable();
System.out.println(" instance variable ="+ instanceVariable.i);
}
}


Output :
instance variable = 20


3. Static Variable :


i. Variable declare inside class with static keyword is called static variable .
ii. Static variable can not local.
iii. Static variable belogs to class ,not to object.
iv. Access modifiers can be used for static variables.
v. Static varible access as ClassName.StaticVariableName

public class StaticVariable {
static int i =100;
public static void main(String[] args) {
System.out.println("price of bag is ="+ i);
}

}

Output :

price of bag is =100




Related Posts:




Exception Hanlding


File Handling