Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Types of Member Methods in Java
Previous Home Next

There are two types of member methods in java

  1. Instance Member Methods/Non-Static member Methods.
  2. Static Member Methods.

Instance Member Methods

  1. Instance method is defined in a class which is only accessable through the Object of the class are called Instance method.
  2. These methods can only be called through objects.
  3. Non-static member are declared without the static keyword.
  4. If we call a instance method inside a static method of same class then also we have to use object name.
  5. When an object calls a method then it is called as message passing and this is feature of oops.
  6. When an object calls a method then the referrence of the object is automatically passed as an argument to the method and this reference will be received by this keyword inside the class.

Syntax of instance method

return-type funcName(paramList)
{
     // Body of this method
}

return-type is the type of answer that the method produces.

Syntax to call instance method

objRef.funcName(args);

objRef is an object reference variable and funcName is the name of the method. args are an optional arguments.

Example :

class Instance
{ 
	int l = 9; //non-static 
	double d = 12.2;//non static 
	void sample() //non-static method 
	{ 
		System.out.println("hello"); 
	} 
public static void main(String args[]) 
{ 
	Instance i = new Instance();//instance creation 
	System.out.println(i.l); 
	System.out.println(i.d); 
	i.sample(); 
	System.out.println("program ends...."); 
} 
} 

output :

9
12.2
hello
Program end....

Static Member Methods

  1. These methods can be called through class name or through object name.
  2. Static methods can access ony static member. Static method can be called directly (without object or class name) from a static method of same class.
  3. This keyword will not work inside a static method because this contains the reference of the calling object but static method is generally called through class name and even if static method is called through object then also the reference of calling object will not passed in the method.
  4. Super keyword will also not work.
  5. Instance member variables can not work directly inside a static method but using object we can access in instance member also.
  6. static method can use only static variables.

Example1 :

class A
{
	int x; // instance member variables
	static void m()
	{
		x=5;
		System.out.print(x);
	}
public static void main(String args[])
{
	A.m();
}
}

output :

Error - Static method can not  access instance variable

Example2 :

class A
{
	int x;  //instance variable
	static void m()
	{
		A a1=new A();
		a1.x=5;
		System.out.print(a1.x);
	}
}
class StaticMethod
{
	public static void main(String args[])
	{
		A.m();
	}
}

Output :

5

Example3 :

class Math
    {
            static int sum(int a, int b)
            {
                    return(a+b);
             }
            static float avg(int a, int b)
            {
                    return((a+b)/2.0f);
            }
    }

  class mathMathod
  {
            public static void main(String args[])
            {
                    System.out.println(Math.sum(5,6));
                    System.out.println(Math.avg(5,6));
                    Math m=new Math();
                    System.out.println(m.sum(5,6));
                    System.out.print(m.avg(5,6));
            }
    }

output :

11
5.5
11
5.5
Previous Home Next