Home » Java programming language

Java Thread Class final String getName() method with Example

Java Thread Class final String getName() method: Here, we are going to learn about the final String getName() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 20, 2019

Thread Class final String getName()

  • This method is available in package java.lang.Thread.getName().
  • This method is used to return the name of this thread.
  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.
  • This method is final so we can't override this method in our program.
  • The return type of this method is String so it returns the name of the thread as a string (i.e. Name will be in the string).
  • This method does not raise any exception.

Syntax:

    final String getName(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is String, it returns the name of this thread as a string.

Java program to demonstrate example of getName() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class GetThreadName extends Thread {
    // Override run() of Thread class
    public void run() {
        System.out.println("The priority of this thread is : " + Thread.currentThread().getPriority());
    }

    public static void main(String[] args) {
        // Creating an object of GetThreadName class
        GetThreadName gt_name = new GetThreadName();

        // We are setting the name of the thread GetThreadName
        gt_name.setName("GetThreadName");

        /*  Calling start() method with GetThreadName class 
            object of Thread class 
        */
        gt_name.start();

        /*  By using getName() method to return the name of 
            this thread [GetThreadName ]
        */
        System.out.println("The name of this thread is " + " " + gt_name.getName());
    }
}

Output

E:\Programs>javac GetThreadName.java

E:\Programs>java GetThreadName
The name of this thread is  GetThreadName
The priority of this thread is :5


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.