Visitor Counter

Fat Burning

Sunday, May 31, 2015

What Is Static Keyword in Java With Example

Java allocates recollection after instantiation via new operator. But there is one exclusion to this rule, static keyword.

Static keyword is used with data and methods of the class:

  • If you want to have only a particular piece of storage for a exacting field, regardless of how many objects of that class are twisted, or even if no objects are shaped.                                    
  •  If you need a method that isn't connected with any particular object.  

static associate of the class are also referred to as class data ( and class methods) or in general class members. Class name is the favorite way to refer to static members of the class, though you can use objects as well. You can't apply static keyword to limited variables.

public class StaticDemo { 
      private static int count; // initialized to default value of int i.e. 0  
  
      public static int getCount() { 
           return count++; 
      } 
  
      public static void main(String[] args) { 
           System.out.println(" Count: " + StaticDemo.getCount()); 
           StaticDemo sd = new StaticDemo(); 
           System.out.println(" Count: " + sd.getCount()); 
      } 
 } 

production :
 Count: 0
 Count: 1
Note, primary call to method, getCount() is by Class name but the second call is during an instance of class. Output returned is dependable. This case proves a point that there is a exacting storage for static data. 

It's debatable if static methods are Object receptiveness as you call methods without creating matter. My offer, avoid using a lot of static methods.



static Initialization and static building block
Below 2 are one and same :
       private static int count = 10;  //static initialization

or

       private static int count;
       static{    //static block
          count=10;
       }      

And you can have multiple statements inside static block.
 


Order of Execution
Below case demonstrate the order of invocation :

public class StaticLoadDemo {  
      static {  
           System.out.println("Class StaticDemo loading...");  
      }  
   
      static final int c = 5; // static initialization  
      static int a;  
   
      static { // static block  
           a = c;  
           System.out.println("value :" + a);  
      }  
   
      StaticLoadDemo() {  
           System.out.println("Constructor");  
      }  
   
      public static void main(String[] args) {  
           new StaticLoadDemo();  
      }  
   
      static {  
           System.out.println("final static block");  
      }  
 }  

Output:
Class StaticDemo loading...
value :5
final static block
Constructor

Important Points:
  • Static block/initialization gets invoked correct after class gets full. Output of above class confirms the same.
  • Static block is called only one time (during the lifetime)
  • If StaticLoadDemo class extends a base class named as Base. Then static blocks of Base class will be invoked first, followed by subsequent subclasses.
  • Order of initialization is static primary, if they haven't been by now initialized by previous object creation, and then non-static component.

Inheriting static component
static components are connected with the class and not the individual object. So if a method is static, it doesn't behave polymorphic ally.

 class Super { 
      public static void printHello() { 
           System.out.println(" hello --base class"); 
      }  
 } 
  
 public class Derived extends Super { 
      public static void printHello() { 
           System.out.println(" hello --derived class"); 
      } 
  
      public static void main(String[] args) { 
           Super s = new Derived(); 
           s.printHello(); 
      } 
 } 
Output: 
hello--base class

Memory allocation and  de-allocation
static methods (in fact all methods) as well as static data are stored in the PermGen part of the heap, because they are division of the reflection data (class related data, not instance related data). 
1.            static primitive variables are stored in PermGen (Permanent Generation) space.
2.            If static data references to an Object; then reference is stored on PermGen but the actual object gets stored in heap (young/old generation).




Thursday, May 7, 2015

OOPS Concept in Java - Example Tutorial

Hi Guy’s
So this blog is all regarding the OOPS concepts. Let’s start from the very basic concept of Object and Class.

More On Concepts

  1. Introduction
  2. Inheritance
  3. Polymorphism
  4. Encapsulation

What is an Object?
An object is a software collection of related state and performance.
Software objects are regularly used to model the real-world objects that you find in daily life. Real-world substance share two individuality: They all have state and behavior.

Some examples of real world object can be : Bike , Chair , Dogs, have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).

Software objects are theoretically comparable to real-world objects:
They too consist of state and connected behavior.An object stores its state in fields (variables in some programming languages) and exposes its performance through methods (functions in a few programming languages).

Methods function on an object’s inside state and serve as the primary device for object-to-object message.

Defeat inside state and requiring all communication to be performed through an object’s methods is known as ( data encapsulation — a fundamental principle of OOP )

What Is a Class?
A class is a blueprint or example from which objects are produced.

What Is Inheritance?
Inheritance provides a great and natural device for organizing and structuring your software.

How to achieve Inheritance in Java ?
extends keyword is used to inherit features of a class.


What Is an Interface?
An interface is a deal between a class and the external world.
When a class tools an interface, it promises to provide the behavior published by that interface. 

What Is a Package?
A package is a namespace for organizing classes and interfaces in a logical manner.
Placing your code into packages makes large software projects easier to manage.



Monday, May 4, 2015

History of Java

Java history is attractive to know. The history of java starts from Green Team. Java group members (also known as Green Team), initiated a innovative mission to build up a language for digital plans such as set-top boxes, televisions etc.
James Gosling

For the green group members, it was an proceed concept at that time. But, it was right for internet programming. Later, Java knowledge as incorporated by Netscape.

At present, Java is used in internet training, mobile strategy, games, Internet-business solutions etc. There are agreed the major points that describes the the past of java.

A) Mike Sheridan, James Gosling, and Patrick Naughton initiated the Java language development in June 1991. The little group of sun engineers called Green group.
B) First planned for small, embedded systems in electronic appliances like set-top boxes.
C) First of all, it was called "Green talk" by James Gosling and file addition was .gt.

( Special Type of Versions )
There are many java versions that has been released.


  • JDK Alpha and Beta (1995)
  • JDK 1.0 (23rd January, 1996)
  • JDK 1.1 (19th February, 1997)
  • J2SE 1.2 (8th December, 1998)
  • J2SE 1.3 (8th May, 2000)
  • J2SE 1.4 (6th February, 2002)
  • J2SE 5.0 (30th September, 2004)
  • Java SE 6 (11th December, 2006)
  • Java SE 7 (28th July, 2011)
Tag: