Static method in java program

Java is one programming languages. In this language, java introduces different methods in programs. Like abstract method, final method, static method, synchronized method etc.

In this blog we will understand basic different of static and non-static method.

We already see main class static methods concepts in our past blogs. Now we see other class static method how to access by main class.

Different between static and non-static methods in java programs

– Static methods directly call by class name, non-static method needs object for access.

 

 

Let’s see example on static and non-static methods in java programs.

class A

{

            // Two Method

            // 1. Static

                        static void method1()

                        {

                                    System.out.println(“Welcome in Static Methods”);

                        }

            // 2. Non-Static

                        void method2()

                        {

                                    System.out.println(“Welcome in Non-Static Methods”);

                        }

}

class clEx11

{

            public static void main(String args[])

            {

                        // How to call static Methods from other class

                        A.method1();    // No Need to create an object for static methods

 

                        // How to call non-static Methods from other class

                        // We need object

                        A a1 = new A();            // a1 is an object          

                        a1.method2();  // call to non-static methods

            }

}

 

Output:

Welcome in Static Methods

Welcome in Non-Static Methods

 

 

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn Money Online

723total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image