secretofreading(Stay Tuned To My Blog For More Updates!!!)(Blog is for sale any one want to buy just fill out the contact form...

Saturday 8 June 2013

Methods In Java

                   

=> A method represents a group of statements that performs a task.
A method contains two parts.
1.Method header (or) prototype:-
This part contains method name,parameters and return type.
syn:- returntype methodname(para-1,para-2,...)
ex:- void sum()
    void sum(double s1,double s2)
2.Method body:-
it represents a group of statements written in {} to perform the task.
syn:-
{
statements;
}

Note:- If a method returns some value,we should use return statement in the method body.
ex:- return k;

Instance method:-
It is a method that acts upon instance varaibles of a class.

ex:-

static method:-
A static method is a method that does not act upon instance variables of a class.static methods are declared by using 'static'       keyword.
=>All static methods can be called by using Objectname.methodname();      (or) classname.methodname();

ex:-

*** Instance variables are not available to static methods.Because JVM     executes static methods first and creates objects later.

Jvm executes in below order.
1.static blocks.
2.staic methods.
3.instance methods.
                                         


*** static variables are available to static methods directly.
    static variables are also declared as static.
ex:- static int i=100;

ex:-

IIQ)what is the difference between an instance variable and static variable?
Instance variable is a variable whose seperate copy is available to every object.Any modifications to the instance variables in one object will not effect other objects.
A static variable is a variable whose single copy in memory is shared by all the objects of the class.so any modification to the static variable will also modify all the objects.
Instance variables are created in heap memory.
Static variables are stored on method area.

NOTE1:-
Any thing static stored on method area.
Note2:-
A static block represents a block of statements declared as        static.
ex:-
static{
statements;
}

Note:- With out main method any class will be compile and run.

this:-
It represents the present class object.it means this      represents all the members of the present class.

        this-->present class instance variables.
   -->present class methods.
   -->present class constructors.

ex:-

Instance Methods:-
----------------
There are two types of instance methods.
1.Accessor methods
2.Mutator methods.
 Accessor methods:-
These are simply access or read the instance variables.They do not modify the instance variables.

Mutator methods:-
These are not only access the instance variables but also     modify them.

ex:- write a program to create Person class object

IIQ)How objects are passed to the methods in JAVA?
primitive datatypes,objects,even object references-every thing is passed to methods using 'pass by value' (or) 'call by value' concept.This means bit by bit copy is passed to the methods.
                                     

ex:-


Factory methods:-
---------------
Factory methods are static methods only.But their intension is to create an object depending on the user choice.
A factory method is a method that returns an object to the class,to which it belongs.
  ex:-  getNumberInstance() is a Factory method.Because it belongs to NumberFormat class and returns an object to NumberFormat class.

IIQ)What are factory methods?
A factory method is a method that creates and returns an object to the class to which it belongs.A single factory method replaces several constructors in the class by accepting different options from the user,while creating the object.


some NumberFormat methods are:
setMaximumIntegerDigits();
setMinimumIntegerDigits();
setMaximumFractionDigits();
setMinimumFractionDigits();
Apply the format to the required statement using format() method.This method returns a string that contains formatted area value.

 //creating NumberFormat class object:
NumberFormat obj = NumberFormat.getNumberInstance();

  There is another way of using getNumberInstance() method as:
getNumberInstance(Locale constant);
   Here,the parameter represents a constant that represents the name of a country(locality) as Locale.CANADA,Locale.CHINA,Locale.FRANCE,...

ex:- w.a.p to calculate the area of a circle.


IIQ)In how many ways can you create an object?
There ae four ways of creating objects in java.
1.Using new operator.
    ex: Employee obj = new Employee();
2.Using factory methods.
    ex:- NumberFormat obj = NumberFormat.getNumberInstance();
3.Using newInstance() method.Here,we should follow two                                                      steps,as
  a)First,store the class name 'Employee' as a string into an object.For this purpose,factory method forName() of the class 'Class' will be useful.
     Class c = Class.forName("Employee");
   here Class is a class in java.lang package.
  b)Next,create another object to the class whose name is in the object c,for this purpose,we need newInstance() method of the class 'Class' as:
   Employee obj = (Employee)c.newInstance();

4.By cloning an already available object,We can create another           object.Creating exact copy of an existing object is called              "cloning".
Employee obj1 = new Employee();
Employee obj2 = (Employee)obj1.clone();




No comments:

Post a Comment