Java.lang.StackTraceElement class in Java
Last Updated :
03 Sep, 2021
An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated.
This class describes single stack frame, which is an individual element of a stack trace when an exception occur.
- All stack frames except for the one at the top of the stack represent a method invocation.
- The frame at the top of the stack represent the execution point of which the stack trace was generated.
- Each stack frame represents an execution point, which includes such things as the name of the method, the name of file and the source code line number.
- An array of StackTraceElement is returned by getStackTrace()
method of the Throwable class.
Constructor: Creates a stack trace element representing the specified execution point.
StackTraceElement(String declaringClass,
String methodName, String fileName, int lineNumber)
Parameters:
- declaringClass - the fully qualified name of the class containing the execution point represented by the stack trace element.
- methodName - the name of the method containing the execution point represented by the stack trace element.
- fileName - the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable
- lineNumber - the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method.
Throws: NullPointerException - if declaringClass or methodName is null.
Methods:
1. boolean equals(ob): Returns try if the invoking StackTraceElement is as the one passed in ob. Otherwise it returns false.
Syntax: public boolean equals(ob)
Returns: true if the specified object is
another StackTraceElement instance representing the same execution
point as this instance.
Exception: NA
Java
// Java code illustrating equals() method
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
StackTraceElement st1 = new StackTraceElement("foo", "fuction1",
"StackTrace.java", 1);
StackTraceElement st2 = new StackTraceElement("bar", "function2",
"StackTrace.java", 1);
Object ob = st1.getFileName();
// checking whether file names are same or not
System.out.println(st2.getFileName().equals(ob));
}
}
Output:
true
2. String getClassName(): Returns the class name of the execution point described by the invoking StackTraceElement.
Syntax: public String getClassName().
Returns: the fully qualified name of the Class
containing the execution point represented by this stack trace element.
Exception: NA.
Java
// Java code illustrating getClassName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("Class name of each thread involved:");
for(int i = 0; i<2; i++)
{
System.out.println(Thread.currentThread().getStackTrace()[I].
getClassName());
}
}
}
Output:
Class name of each thread involved:
java.lang.Thread
StackTraceElementDemo
3. String getFileName(): Returns the file name of the execution point described by the invoking StackTraceElement.
Syntax: public String getFileName().
Returns: the name of the file containing
the execution point represented by this stack trace element,
or null if this information is unavailable.
Exception: NA.
Java
// Java code illustrating getFileName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("file name: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
getFileName());
}
}
Output:
file name:
Thread.java
StackTraceElementDemo.java
4. int getLineNumber(): Returns the source-code line number of the execution point described by the invoking StackTraceElement. In some situation the line number will not be available, in which case a negative value is returned.
Syntax: public int getLineNumber().
Returns: the line number of the source line
containing the execution point represented by this stack
trace element, or a negative number if this information is
unavailable.
Exception: NA.
Java
// Java code illustrating getLineNumber() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("line number: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
getLineNumber());
}
}
Output:
line number:
1556
10
5. String getMethodName(): Returns the method name of the execution point described by the invoking StackTraceElement.
Syntax: public String getMethodName().
Returns: the name of the method containing the
execution point represented by this stack trace element.
Exception: NA.
Java
// Java code illustrating getMethodName() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("method name: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
getMethodName());
}
}
Output:
method name:
getStackTrace
main
6. int hashCode(): Returns the hash code of the invoking StackTraceElement.
Syntax: public int hashCode().
Returns: a hash code value for this object.
Exception: NA.
Java
// Java code illustrating hashCode() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("hash code: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
hashCode());
}
}
Output:
hash code:
-1225537245
-1314176653
7. boolean isNativeMethod(): Returns true if the invoking StackTraceElement describes a native method. Otherwise returns false.
Syntax: public boolean isNativeMethod().
Returns: true if the method containing the execution
point represented by this stack trace element is a native method.
Exception: NA.
Java
// Java code illustrating isNativeMethod() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
isNativeMethod());
}
}
Output:
false
false
8. String toString(): Returns the String equivalent of the invoking sequence.
Syntax: public String toString().
Returns: a string representation of the object.
Exception: NA.
Java
// Java code illustrating toString() method.
import java.lang.*;
import java.io.*;
import java.util.*;
public class StackTraceElementDemo
{
public static void main(String[] arg)
{
System.out.println("String equivalent: ");
for(int i = 0; i<2; i++)
System.out.println(Thread.currentThread().getStackTrace()[i].
toString());
}
}
Output:
String equivalent:
java.lang.Thread.getStackTrace
StackTraceElementDemo.main
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read