Header Ads Widget

Ticker

3/recent/ticker-posts

METHOD STATIC IN JAVA FULL DETAILS

 

Methods-static

-Static methods apply to the class as a whole rather than an
instance of the class

-Static methods can be invoked using the same notation as non
static methods
-However it is more appropriate to invoke static methods using
the class name rather than a variable name

-To declare a method as static, place the keyword static before
the return type in the method’s declaration.

-Note that a static method can call only other static methods of
the same class directly (i.e., using the method name by itself) and can manipulate only static fields in the same class directly.

-Java does not allow a static method to access non-static
members of the same class directly.
-To access the class’s non-static members, a static method
must use a reference to an object of the class.

Methods
example, no parameters, no return type
Public class MyClass
{
public static void main(String args[])
{
myMethod();
}
public static void myMethod()
{
System.out.println("Hallow Java");
}
}

Methods
example, Parameters
Public class MyClass{
public static void main(String args[]){
myMethod("said");
add(23,56);
add(45,89);
add(56,577);
add(34,67);
}
public static void myMethod(String name){
System.out.println("hellow " + name);
}
public static void add(inta, intb){
System.out.println(a + b);
}
}

Methods
example, Returning a value
public class MyClass{
public static void main(String args[]){
int sum = add(23,56);
System.out.println(sum);
}
public static void myMethod(String name){
System.out.println("hellow " + name);
}
public static int add(inta, intb){
return(a + b);
}
}

Post a Comment

0 Comments