Using Class Methods

This is an example of how to use methods in different classes.

Java methods are methods in a class used to perform actions.

They can either be static or public.

Static Methods can be accessed in a class without creating an object.

Below is an example on using a Static Method. I created a class called UsingClassMethods.

public class UsingClassMethods
{  

Then I created a static method called testStaticMethod.

    static void testStaticMethod()
    {

Finally I call the static method testStaticMethod from the main method.

testStaticMethod();

Below is the hole code put together.

public class UsingClassMethods
{  
    //Since this method is static it can be accessed in this class without creating an object of this class
    static void testStaticMethod()
    {
        System.out.println("This is called from a static method in UsingClassMethods\n");
    }

    public static void main(String[] args)
    {
        //This is called from a static method in this class, UsingClassMethods
            testStaticMethod();
    }
}

For Public Methods you need to create an object of the class to access them. Below is an example of an Object called testClassObject that uses the class UsingClassMethods.

        //This creates a new object of this class, UsingClassMethods
        UsingClassMethods testClassObject = new UsingClassMethods();

To access methods in an object you need to first write the object’s name with a dot or .

You can also create an object of a class and access it from another class in a different file. Below you can find my user file. This will be the class used to create an object. It contains a String name and two public methods. The first one setUsername to set the value and the following getUsername to retrieve the value.

//This is the User class as its own file
//It consists of a name string and two methods one to set it and one to retrieve it
public class User
{
    String name = "";

    public void setUsername(String tempName)
    {
        name = tempName;
        System.out.println("The string has been sent to the User class method setUsername\n");
    }


    public void getUsername()
    {
        System.out.println("This is from the User class method getUsername and this is the string sent from UsingClassMethods: " + name +"\n");
    }
}

Finally this is an example of using the User class as an object.

//Class Methods are used to call methods between different classes
public class UsingClassMethods
{
        //This creates an object of the class User called aUser
            User aUser = new User();
            System.out.println("This creates an object of a class called User\n");


        //This is calling the setUsername method from the object called aUser
            aUser.setUsername("Greg");


        //This calling the getUsername method from the object called aUser
            aUser.getUsername();
    }
}

A question to ask yourself. What is the difference between using static and public on methods?

Leave a comment

Design a site like this with WordPress.com
Get started