Home     Blog

Java Method

A Java method is a set of Java statements that can be included inside a Java class.

Java methods are similar to functions or procedures in other programming languages.

There are many different types of Java methods:

Final Method
Public Method
Private Method
Package Method
Instance Method
Protected Method
Static Method
Main Method

The Main Method

Every Java program must have one main method. The main method is the first method which the Java Virtual Machine executes. The main method then calls the other methods, as they are needed. Main method is often written as main() method.

Here is the main() method from a Java program that prints “Hello World:”

public static void main (String[] args) {
// This Java program prints "Hello World!"
System.out.println{"Hello World!");
}

It is good programming practice to maintain a very small main method and to move all possible code into other methods.

 Java Method

Instance Method

An instance method is a method which is associated with one object and uses the instance variables of that object.

The opposite of an instance method is a static method.

The default for a method is to be an instance method.

Static Method

A static method is a Java method which accepts arguments instead of using instance variables.

The opposite of a static method is an instance method.

The default for a method is to be an instance method. Methods must be explicitly defined as static methods.

A static method is also referred to as a class method.

Package Method

A package method can be called by any class within its package.

This is the default access level if no access level is explicity declared.

The other options for declaring visibility are private, public, and protected.

Private Method

A private method is a method which is not inherited by subclasses.

Declaring a method private defines its access level.

The other options for declaring visibility are public and protected.

If undeclared, the default access level is package.

Protected Method

A protected method can be called by any subclass within its class, but not by unreleated classes.

Declaring a method protected defines its access level.

The other options for declaring visibility are private and public.

If undeclared, the default access level is package.

Public Method

A public method is a method which can be called by any object.

Declaring a method public defines its access level.

The other options for declaring visibility are private and protected.

If undeclared, the default access level is package.

Final Method

A final method is a method which cannot be overridden by a subclass.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Comments (1)

 

  1. lol says:

    hmmm… your main method has a problem…

    it should be

    System.out.println(“Hello World”);

    not

    System.out.println{“Hello World”);

    btw any examples on instance methods?

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • How to Declare a Constant in Java

    Java does not directly support constants. However, a static final variable is effectively a constant. The static modifier causes the variable to be available without loading an instance of the class where it is defined. The final modifier causes the variable to be unchangeable. Naming Standards for Java Constants Java constants are normally declared in [...]...


  • Inheritance

    Inheritance is what happens when a subclass receives variables or methods from a superclass. It is also when one class passes down its properties to another class. The main reason for inheritance is reusability. Distinguishing a new class from an existing class is called derivation. The old class is referred to as the base class [...]...


  • Java Class

    A Java class is a group of Java methods and variables. Each Java source code file can contain one public class. The name of this public class must match the name of the Java source code file. If the public class is called “ballistics,” then the filename would be “ballistics.java.” Example Java Class class Ammunition [...]...


  • Java Package

    A Java package is a set of classes which are grouped together. This grouping helps to organize Java classes and codevent multiple Java classes with the same name. Using Java Packages To use a package in your Java source code, you must either import the package or use the fully qualified class name each time [...]...


  • Java vs. C++

    C++ supports pointers; Java does not (Java does have object references) C++ supports mutiple inheritance directly; Java supports multiple inheritance only through interfaces C++ supports operator overloading; Java does not C++ supports global variables; Java does not (sort of) C++ supports #define; Java does not C++ supports typedef; Java does not C++ supports enum; Java [...]...