MainMenu

Home Java Overview Maven Tutorials

Sunday 15 January 2017

Java Object Class & Method with example






For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Java Object Class & Method with example

For Video : CLICK HERE




Class: A group of object with common properties known as class and A class is a template from which objects are created.


A class in java can have :-
1).Fields
2).Contructor
3).Methods
4).Blocks
5).Nested Class and Interface

Object:Object is an Instance of a class which have state and behavior.

Every thing which are near to us is an object. For example : Pen, Car, Dog etc.
If we take car as an example then Car is an object which have state like Name, Color & behavior is running, making horn etc.
Create object :Object can be created with "New" operator. Syntax : Class_name Object_name = new Class_name();

Class :

A class is the blueprint from which individual objects are created also class describe the behavior and state of objects that he support. In above Car example the class will be "CAR or Four Wheeler".
A class can have a three type of variable : Local variables: Which are defined inside method, constructor or blocks.
Instance variables: Which are within the class but outside the method. These variables are initialized when the class is instantiated.
Class Variables : Variable which are declared with the Static method and created within the class and out side of method.

Method in Java:

Method are basically a set of code which are used to perform some operation when invoked. method can be created with or without return values & invoked with or without parameters. Syntax : modifier returnType nameOfMethod (Parameter List)
{
// method body
}
Example : public static int chandanmethod1(int a, int b)
{
// method body
}

Example of class, object & method in java program:

public class chandan1 //class
{
void sum(int a, int b) //method
{
System.out.println("sum is :" +(a+b));
}
}
public class chandan2
{
public static void main (string args[])
{
chandan1 obj = new chandan1(); //method
obj.sum(3,4);
}
}

Output: Sum is : 7

innner Class In Java

package com.lang.lecture2;
class A
{
public void g()
{
System.out.println("I am inside g");
}
class B
{
public void h()
{
System.out.println("I am inside h");
}
}
}
public class lec2
{
public static void main(String args[]) {
A obj = new A();
obj.g();
A.B obj2 = obj.new B();
obj2.h();
}
}


Output :- I am inside g
I am inside h

Tags:What is class object & method in java, example of class method & object in java




No comments:

Post a Comment