Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:06:08+00:00 2026-06-10T15:06:08+00:00

How do I figure out what class called my method without passing any variable

  • 0

How do I figure out what class called my method without passing any variable to that method?
let’s say we have something like this:

Class A{}
Class B{}
Class C{
public void method1{}
System.out.print("Class A or B called me");
}

let’s say an instance of Class A calls an instance of class C and the same for class B. When class A calls class C method1 method, I want it to print something like “Class A called me”, and when class B called it to print “Class B called me”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:06:09+00:00Added an answer on June 10, 2026 at 3:06 pm

    There’s no really easy way to do this, because normally a method doesn’t and shouldn’t need to care from where it is called. If you write your method so that it behaves differently depending on where it was called from, then your program is quickly going to turn into an incomprehensible mess.

    However, here’s an example:

    public class Prut {
        public static void main(String[] args) {
            example();
        }
    
        public static void example() {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            StackTraceElement element = stackTrace[2];
            System.out.println("I was called by a method named: " + element.getMethodName());
            System.out.println("That method is in class: " + element.getClassName());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how to catch any method called on an
I'm trying to figure out the best way to create a class that can
I can't figure out how to call a base class method from a derived
I'm trying to figure out if this is possible. I have a class, BaseGameEntity,
Trying to figure out the best way to do this (And without crossing any
I have a challenge in my C# .NET application that I can't figure out
I have created a class called aptSchedule which, like its name suggests, keeps track
I have to develop something like a game of life. For that, I have
I can't figure out the proper block initialize class Foo attr_accessor :bar end obj
I was trying to figure out what happens when a derived class declares a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.