Methods in Java

Click to rate this post!
[Total: 1 Average: 4]

Methods in Java:

A Java method is having   statements that are combined together to perform an action. When  invite  the System.out.println() method.

Creating Method:

For creating a method we must have the same method header and method body


public static int methodName(int a, int b){



Here Public static is an modifier ,int is a return type, a and b are Parameters

Method Calling:

For calling a method there are two ways that is return a value and no return value.  When a method calling a program invokes a method, and control by called method then returns control to the caller in two conditions, when the return statement is executed, it reaches the method ending closing brace.


The getArea() method in the Square class that was explained in the below on objects returns an integer:

// a method for computing the area of the square    Public int getArea() {        return width * height;    }

File and Input/ Output Stream:

This class can be used for performing and reading operation from a resource byte by byte

Syntax: DataInputStream dis= new DataInputStream(InputStream);


This class can be used for reading the data from a file byte by byte and BufferedInputStream is a altered stream using which we can improve the performance of the application.


FileInputStream fis= new FileInputStream(File);

FileInputStream fis= new FileInputStream(string);

BufferedInputStream bis= new BufferedInputStream(InputStream);


Import java .io.*;

Public class FileReading{

Public Static Void main (string [ ] ar) throws IO Exception {

FileInputStream fis = new FileInputStream (“input.txt”);

BufferedInputStream bis= new BufferedInputStream (fis);

Int ch ;

While ((ch=bis. read ( ) ) ! =-1) {

System.out.println((char)ch); }

bis.close( );



FileOutputStream: It is used for writing the content into a file byte by byte

Syntax: FileOutputStream fos= new FileOuputStream(File);

FileOutputStream fos= new FileOuputStream(string);


An Exception is a Runtime Error which occurs because of logical failure or invalid input or wrong data. The exception class is super most class of all Exceptions.

The Exceptions are classified into two types

  • Compile time Exception
  • Run Time Exception

The runtime errors are further classified into two types based on the whether there are handled or not

  • Checked Exception
  • Unchecked Exception

According to the checked exception these were occurred at the compilation time and the user is handled this checked exception by skipping it. And coming to the unchecked exception are occurs at the time of execution. In a program it includes errors, bugs are ignored at the time exception.

Exception Hierarchy: The exception classes are divided into some categories of the java.lang.Exception class. There is another class called Error which is comes from the Throwable class. In java programs errors are irregular conditions that come in failures, these are not handled.

JAVA Tutorial Video

The Exception class has two main subclasses:

If a class is specified inside anther class then it is called inner class or nested class.The inner classes are the helper classes to the outer classes based on the location of the inner classes the keyword is specified.

  • Regular inner class
  • Method inner class
  • Anonymous inner class
  • Static inner class

If a class is declared inside another class without any keyword then it is known as regular class. The regular inner class can be accessed directly inside the instance method of outer

c lass because by the time control comes into the instance method of outer class an object of outer class is already created. The regular inner classes will exist only if an object of outer class exist.

If a inner classes exists inside a method then it is called as method local inner class. If a method local inner class can be used only it that method where it is declared and it should be used after declaration. If a inner class does not have any name then it is called anonymous inner class, if a class is declared inside another class with static keyword then it is in inner class, it exists without outer class object.


Class Outer {

Static Class Inner {

Public class void main (String [ ] ar) {

System.out.println(“inner class main method”);

} }

Public static void main (string [ ] ar) {

System .out.println(“Outer class main method”);

} }


Class Outer


Class Inner

Void inner Method( ) {

System.out.println(“inner method”);



Void Outer Method( ){

Inner i=new Inner( );

  1. inner Method( );

System.out.println(“Outer Method”);


Public static void main (string[ ] ar ){

System.out.println(“outer class main method”);

Outer O= new Outer( );

O.Outer Method( );



Multi Threading:

Thread is a piece of code that executed separately if a program contain multiple threads then it is called as multithreading and they language which supports multi threading will be called as multi threaded language.

Every java program will by default contain one thread called as main thread which is responsible for executing the java program.

Life Cycle of Thread Class:

Life Cycle of Thread


Procedure to create a thread by extending thread class:

  • Declare a class which extends from thread class
  • Specifying the functionality of user defined in run ( )
  • Create an object of a class which extends from thread class
  • Attach the above created object to the main thread
  • Execute the functionality of user defined thread available in run( ) by using start( )

Procedure to create a user defined thread by implementing Runnable interface:

  • Declare a class which implements runnable interface
  • Specifying the functionality of user defined thread in run( )
  • Create an object of that class which implements runnable interface
  • Attach the above created object to the main thread
  • Execute the functionality of user defined thread available in run( ) by using start ( )

Methods of Thread class:

It will provide the information of currently executing thread. This method can be used to suspend the execution of the thread for the specified amount of milli seconds. The Sleep( ) method will through interrupted exception which must be handled because it is checked exception.

Methods of Thread Class


It is an object which can store group of other objects every object requires 10 bytes of memory and to store 4 objects .we require 40 bytes of memory .Hence 4 objects are stored one time inside the collection object and one time outside the collection object and wasting the memory by duplication.

  • To overcome of the array concept it drawbacks the sun Micro systems has released collections in java 2 version.
  • In order to avoid the wastage of memory the JVM will store the references of the objects instead of storing the objects directly.

 Collection Interface:

The abstract data which represents collection Interfaces to allows independently in details of their illustration in object-oriented languages.

The methods perform the action of algorithm by through searching and sorting in the process ,the algorithm should be in many forms and same methods.


  • This collection is a combinations multiple objects ,it stores instance of an object to handle the set.
  • Collection object is a collection the references of other objects and class whose object can store group of references of other objects.

to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button