Please open the menu to show more

Wednesday, July 19, 2017

Modifiers, The very very important concept in java

let me provide some information about the package in java :


basically package is a directory in which we store the classes.
by using package we can distinguish the classes

a package may be predefined like java.util or it may be created by developer




now i will provide some basic information about modifiers in java :



basically the modifiers are some keywords (pre defined words) those can be used by the developers
to describe the accessibility and behavior of a class or variable or a method or a constructor

there are 12 modifiers in java, and they are given below


1. private

2. package private [this is a default modifier, we cant apply it by our self]
3. protected
4. public 
5. final
6. static
7. transient
8. volatile
9. abstract
10. native
11. synchronized
12. strictfp

# private modifier can be applied on a variable/method/constructor

# package private modifier is applicable for class/variable/method/constructor
# protected modifier can be applied on a variable/method/constructor
# public modifier can be applied on a class/variable/method/constructor
# final modifier can be applied on a class/variable/method
# static modifier can be applied on a variable/method
# transient and vol modifiers can be applied on a variable only 
# abstract and strictfp modifiers can be applied on a class/method
# native and synchronized modifiers can be applied on a method only

now i will describe what is the meaning of each modifier -


## a private variable/method/constructor cant be accessed outside the class in which they are defined


## a package private class/variable/method/constructor can be accessed only inside the package in which they are defined


## a protected variable/method/constructor can be accessed inside the package, but they can also be accessed outside the package via inheritence (i mean they can be accessed out of package in subclasses only)


##  public => a public class/variable/method/constructor can be accessed everywhere 


note : the public class name and the file name in which it is defined must be same.

  So, a java program cant have more than one public class  
  
  public class Foo // this class must be saved inside Foo.java file
  {
  }
  
  public class Doo // this class must be saved inside Doo.java file
  {
  }

##  final class => cant be inherited but its object can be created


final class Foo

{
}

class Doo extends Foo // this is illegal, cant inherit final class
{
}

Foo f = new Foo(); // it is legal, object of final class can be created

## final variable => final variable is treated like a constant, so they cant be modified

    class Foo

{
final int x = 1;
void function()
{
int y = x; // correct
x++; // incorrect, since value of 'x' cant be changed
}
}

## final method => final methods cant be overridden by the subclass, means if a super-classes apply final modifier on its method, then we cant define the same method in subclass

class Foo

{
final void function() // this method cant be defined by subclass
{
}
}

class Doo extends Foo
{
void function() // it is an error, overriding not allowed
{
}
}

## static => static variable and static method can be accessed using class name, no need to create object of a class in order to access static variable/method

class Foo

{
static int v = 1;
static void fx()
{
}
}

class Doo
{
public static void main(String[] args)
{
// look here, we are using variable 'v' and method 'fx' using class name
System.out.println(Foo.x);
Foo.function();
}
}

## transient variable => transient variables cant be saved inside the hard disk (i mean they cant be serialized),

they live in RAM and they store sensitive information

## volatile variable => volatile variable do enjoy the atomic operations in multi-threaded environment


## native method => native methods are awesome, since they are declared in java application, and defined by c and c++ generally, after that the java application can invoke these methods, mainly the native methods are used to access the OS and hardware directly, one more thing, we cant provide body (i mean implementation) of native methods in java


public class Foo

{
public native void fx(); // look here, there is no body provided (i mean {})
}

## synchronized method => a synchronized method can be accessed by a single thread at a time. Thread is a part of program.


public class ATMCabin

{
// this atmMachine can be accessed by a single thread (i mean by a person) at a time
public synchronized void atmMachine()
{
}
}

## abstract class => abstract class can be inherited but its object cant be created
 
abstract class Foo
{
}
 
class Doo extends Foo // correct, can be inherited
{
}
 
Foo f = new Foo(); // incorrect, object cant be created
 
## abstract method => abstract method will be just declared inside a class, and its implementation will be given by the subclass, an abstract method can be declared inside abstract classes only
 
abstract class Foo
{
// look here, the method is declared, here is no body provided
// its body will be given by the subclass
public abstract void add(int x,int y);
}
 
class JFoo extends Foo
{
// provide implementation for abstract method, which is declared inside the super-classes
public void add(int x,int y)
{
int result = x+y;
}
}

## strictfp => it can be applied on a class or a method. it is used to ensure a platform independent float point arithmetic operations


note : only final modifier can be applied to the local variables


class Foo

{
void method()
{
int a = 1; // it is a simple local variable
final int b = 2; // it is a final local variable, it cant be modified
}
}

10 comments:

  1. Excellent website. Lots of useful information here, thanks in your effort! . For more information please visit.............
    medical coding training and placements in hyderabad

    ReplyDelete

  2. awesome post presented by you..your writing style is fabulous and keep update with your blogs.Evaluation and Management training in Hyderabad

    ReplyDelete
  3. It was really good.. easily understandable...
    The presentation was simple nd splendid....

    ReplyDelete
  4. no one is better than u.

    ReplyDelete
  5. Nice Sir , all important concepts given here

    ReplyDelete
  6. Sir FileClass ka concept post kr dijiye plz...

    ReplyDelete