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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.