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
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();
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
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
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
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
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
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
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
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.util.ArrayList.add() Method in Java
The add() method in the ArrayList class is used to add elements to the list. There are different versions of this method. Example 1: In this example, we will use the add() method to add elements at the end of the list. [GFGTABS] Java // Java Program to demonstrate Addition of // Elements to an Array
2 min read
Reverse a stack without using extra space in O(n)
Reverse a Stack without using recursion and extra space. Even the functional Stack is not allowed. Examples: Input : 1->2->3->4 Output : 4->3->2->1 Input : 6->5->4 Output : 4->5->6 We have discussed a way of reversing a stack in the below post.Reverse a Stack using Recu
6 min read
Stack search() Method in Java
The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than o
2 min read
Stack Class in Java
The Java Collection framework provides a Stack class, which implements a Stack data structure. The class is based on the basic principle of LIFO (last-in-first-out). Besides the basic push and pop operations, the class also provides three more functions, such as empty, search, and peek. The Stack cl
12 min read
Stack addElement(E) method in Java with Example
The addElement(E) method of Stack Class is used to append the element passed as a parameter to this function at the end of the Stack. Syntax: boolean addElement(E obj) Here, E is the type of elements maintained by this container. Parameters: This function accepts a parameter E obj which is the objec
2 min read
Stack firstElement() method in Java with Example
The Java.util.Stack.firstElement() method in Java is used to retrieve or fetch the first element of the Stack. It returns the element present at the 0th index of the Stack Syntax: Stack.firstElement() Parameters: The method does not take any parameter. Return Value: The method returns the first elem
2 min read
Stack insertElementAt() method in Java with Example
The Java.util.Stack.insertElementAt(element, index) method is used to insert a particular element at the specified index of the Stack. Both the element and the position is passed as the parameters. If an element is inserted at a specified index, then all the elements are pushed upward by one and hen
2 min read
Stack add(Object) method in Java with Example
The add(Object) method of Stack Class appends the specified element to the end of this Stack. Syntax: boolean add(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the Stack. Return
2 min read
Java Program to Implement Stack API
A stack is a linear data structure that follows a particular order in which insertion/deletion operations are performed. The order is either LIFO(Last In First Out) or FILO(First In Last Out). Stack uses the push() function in order to insert new elements into the Stack and pop() function in order t
3 min read
Java Program to Print Stack Trace
Consider a bookshelf in which books are placed one over another. Now if the person wants to read the book which he placed first, in order to do so all books over the first book are needed to be removed to reach out to the desired book. That is the book that was placed first at the beginning itself.
3 min read