Friday, 17 July 2015

STATIC PROPERTIES AND NON STATIC PROPERTIES IN JAVA WITH ONE EXAMPLE


Ex: in a class there are two types of people
1. Boys
2. Girls
It means there are two categories
Category is nothing but class
Now take the boy category. In this category there are so many boys .they are nothing but objects
For each and every boy there are same or common properties and different properties .and there are some common actions and different actions
The common properties between them are no of legs, no of hands
The common actions are sleeping, breathing etc
Static                                                             non static
No of hands                                                 name
No of legs                                                     colour
Sleep ()                                                       interested game
Breath ()                                                  class of study                                       
Static means they are common properties for all the objects
Non static means these properties are different from one boy to other boy
For each and every object there must be a memory location to store the properties
New boy (): it is used to create a memory location
For static there is no need of special memory location
*create 3 memory locations
                    New boy ();
                    New boy ();
                  New boy ();
*after creating the memory locations, if we want to store and retrieve the data from that memory location then that memory location needs one reference name
*by using that reference name only we can store or retrieve the data
Now we are giving the reference names b1,b2,b3 to those memory locations
Boy b1   =    new boy ();
Boy b2 =    new boy ();
Boy b3 =    new boy ();
Class name    reference name   = new class name
For all the boys, no of hands and no of legs are same. These are the common properties of all the boys
For all the boys sleeping and breathing are common actions.in java we call actions as functions or methods
HOW TO DECLARE NON STATIC VARIABLES
If I want to store name then we will declare like below
String name;   datatype   non static variable;   string colour
If we want to store the values into the variables
HOW TO DECLARE STATIC VARIABLES
For static variables we will declare the values like below
Noofhands =2;
    Or
Boy.noofhands =2
Classname.variable name = value
There must be no space between the variable characters,if we want to separate then we can give under scores
No_of_hands =2;
HOW TO ASSIGN VALUES TO NON STATIC VARIABLES
For non static variables we can’t declare the values simply as static variables
    B1.name = “akhilreddy”
    B1.colour =   “white”
B1.intrested game = “cricket”
Then after assigning the values to variables then these values are stored in a corresponding memory location
HOW TO DECLARE STATIC FUNCTIONS:
In the above examples the static functions are breathe and sleep
   Public static void breath ()
{
//body of the function
}
Function means it does some action and returns a value .in some cases it does return a value
If we want to return a value…. the statement is
    Public static data type breath ()
{
}
If we write void in the above statement in the place of data type then meaning of the statement is the function doesn’t going to return any value.
      Public static void breath ()
{
System.out.println (“logic for breathing”)
}  
In the body of the function we have to write the logic
HOW TO DECLARE NON STATIC FUNCTIONS
In the above example the non static functions are play and study
Public void play ()
{
System.out.println (name+”playing”+interested game)
}
For concatenation in java we use ‘+’ symbol .we should not give ““to name, intrestedgame, because these are the variables
HOW TO DECLARE STATIC VARIABLES
In the above example the static variables are no of legs, no of hands
Public static int noofhands:
ACCESS MODIFIERS:
PUBLIC
PRIVATE
DEFAULT
PROTECT
In the above declaration statement we gave public, it means the variable can be accessed throughout the project, in any package, in any class
  Private static int noofhands:
Develop a java program for generating the following o/p in the console

         
package Pack1;

public class boy {
          public static int no_of_hands;
          public static int no_of_legs;
          public String Name;
          public String Color;

          public static void breath()
          {
                   System.out.println("Logic for breathing goes here");
          }
          public static void sleep()
          {
          System.out.println("Logic for Sleeping goes here");      
          }

          public void play()
          {
                   System.out.println("Logic for playing goes here");
          }
          public void study()
          {
                   System.out.println("Logic for study goes here");
          }
         
          public static void main(String[] args) {
                   breath();
                   no_of_hands = 2;
                   no_of_legs = 2;
                   boy p1 = new boy();
                   p1.play();
                   p1.study();
                  
                  
          }

}


Object oriented programming language:
Any object in this world belongs to some class
Every object will have a set of properties and it can do some actions
Properties are divided into two types
Static properties
Non static properties
Actions are also dived into two types
Static action
Non static action
Static properties:  if at all the properties are common to all the objects in that class then we call them as static properties.
Non Static properties:  if at all the values of properties are changing form object to object in that class then we call them as non static properties.
Static actions (static functions): If at all some actions are commonly done by all the objects in that class then we will call them as static functions.
Non static actions (non static functions): If at all the objects are doing the actions which are changing from object to object in that class then we will call them as non static functions.
Note1: Static stub can be directly used in the program.
Note2: Non static stub can be accessed only through reference of the object.
Constructor:  constructor is something which is used for constructing an object very easily
Syntax:  [modifier] ClassName([datatype argument1, datatype argument2,…..]) {
                   Propertyname1= argument1;
Propertyname2= argument2; . . . . .  }        
E.g.:  public boy(String n,String c,String igame,String cos) {
name=n;
colour=c;
          ins_game=igame;        
cls_of_stdy=cos;
}

No comments:

Post a Comment