
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When can we use StackWalker.getCallerClass() method in Java 9?
Java 9 has provided an efficient way of stack walking for lazy access, filtering stack trace using StackWalker API. An object of StackWalker can allow us to traverse and access to stacks. This class contains some useful methods like walk(), forEach(), and getCallerClass().
The getCallerClass() method returns the class that invokes the method that calls this method. To get hold of calling class instance, we need RETAIN_CLASS_REFERENCE while getting StackWalker instance. RETAIN_CLASS_REFERENCE retains an instance of all classes walked by StackWalker.
Syntax
public Class<?> getCallerClass()
Example
import java.lang.StackWalker.Option; public class StackWalkerTest { public static void main(String args[]) { StackWalkerTest1.test1(); } } class StackWalkerTest1 { protected static void test1() { StackWalkerTest2.test2(); } } class StackWalkerTest2 { protected static void test2() { System.out.println(StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).getCallerClass()); } }
Output
class StackWalkerTest1
- Related Articles
- When can we use the pack() method in Java?
- When can we use the getClass() method in Java?
- When can we use intern() method of String class in Java?
- When can we use a JSONStringer in Java?
- When can we use Synchronized blocks in Java?
- When to use the readAllBytes() method of InputStream in Java 9?
- When to use the readNBytes() method of InputStream in Java 9?
- When to use the delayedExecutor() method of CompletableFuture in Java 9?
- Can we use private methods in an interface in Java 9?
- When to use the ofNullable() method of Stream in Java 9?\n
- Can we use "this" keyword in a static method in java?
- How can we use a diamond operator with anonymous classes in Java 9?
- How can we use this and super keywords in method reference in Java?
- Can we have a private method or private static method in an interface in Java 9?\n
- When to use fillInStackTrace() method in Java?

Advertisements