0% found this document useful (0 votes)
17 views

Class 2 - Methodes in Java

Methods in Java allow code reusability and performing tasks. A method must have a meaningful name and cannot be defined within another method. There are two types of methods: system defined (like main and println) and user defined. The main method is called automatically to start program execution. User defined methods can be static, called directly without an object, or non-static, requiring an object to call. Static methods allocate memory at compile time while non-static methods allocate at run time.

Uploaded by

Harshit Ranaware
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Class 2 - Methodes in Java

Methods in Java allow code reusability and performing tasks. A method must have a meaningful name and cannot be defined within another method. There are two types of methods: system defined (like main and println) and user defined. The main method is called automatically to start program execution. User defined methods can be static, called directly without an object, or non-static, requiring an object to call. Static methods allocate memory at compile time while non-static methods allocate at run time.

Uploaded by

Harshit Ranaware
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Methods in java

It is a block of code or group of statements to perform certain


task/operation.
 Code Reusability purpose
 Can not create one method inside another method.
 Can call any method inside another method.
 Method must have meaningful name

Method declaration

public static void methodName()


{

System.out.println(“ xyz ”);

}
Types of methods
1) System Defined Method / Pre defined Method
 Already defined in java library

Main Method Other Pre-defined Methods

public static void main(String[] args) 1) println();


{
2) print()
System.out.println(“ ABC ”);
3) length();
}
4) equal();
 It is called by jvm automatically when we run program
 Code execution start from main method
2) User defined Methods/Regular Methods
 Can not execute automatically
 To execute need to call in main method or any another method

Regular Method

public static void firstMethod()


{

System.out.println(“ ABC ”);

Two Types

1) Static Method
2) Non-static Method / instance method
1) Static Method 2) Non-static Method / instance method

public static void firstMethod()


public void firstMethod()
{
{
System.out.println(“ ABC ”);
System.out.println(“ ABC ”);
}
}

 Static method called directly in any method  Non-static method can not called directly with name
 Static method does not require Object to call  To call Non-Static method need an Object.
 Memory allocation for static method at the time  Memory allocation for non-static method at the time
of compilation. of run
Key Point
 If programs stuck in infinite loop, at the end it will throw stackOverFlowError.
 Allocated call Stack memory exceed.

You might also like