Accesing a reference variable from inside a method java

Peregrine Lennert

I am looking to add a function to a program where I use static variables to create a list of all the times the driver has used the constructor, using names. What I need to know is this, is there a way, in java, to access what the reference variable is (as a string) to add it to the list? Pseudocode:

    public ClassName
    String static list = "";  
    Public ClassName (parameters){
      list += getReferenceVariable();
    }

The getReferenceVariable is what I'm asking if anyone knows a way to do that

Zionsof

If I understand you correctly, you want to keep a list of all the times the constructor was called, and save the names of the currently-being-created variable? Because the "Reference variable" is none when you use the constructor, since you call the constructor with a new MyClass(), and not some obj.MyClass().

If, however, you simply want to know who called you (As a stack trace is), you can simply, as written in this thread (no pun intended), use Thread.currentThread().getStackTrace(), and then choose the desired stack frame (Probably 2, since The first element (index 0) in the array is the java.lang.Thread.getStackTrace method, the second (index 1) is the constructor, and 2 is where the constructor was called from), where you can get (for example) the name of the source file that this stack trace corresponds to. Documentation of getFileName()

Since I haven't tried it on my end (not possible at the moment), I give you code to use with caution:

public class MyClass(){
    MyClass(){
        callerName = Thread.currentThread().getStackTrace()[2].getFileName();
        ... // anything here
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accesing private module variable from class

From Dev

Accesing private module variable from class

From Dev

Variable method reference in java 8

From Dev

Assigning reference returned from method to variable

From Dev

Java 8: Is it possible to assign a method reference to a variable?

From Dev

Java 8 Lambda Sort With Variable Method Reference

From Dev

Accesing staic variable from static function C++

From Dev

java8: method reference from another method reference

From Dev

variable scope inside of parametrized method - java generics

From Dev

static variable inside static method in java

From Dev

Accesing a variable in a ggplot function

From Dev

Accesing a variable in a ggplot function

From Dev

Accesing javascript variable in angularjs

From Dev

Accesing variable in nodejs module

From Dev

Is variable when accessed from outside the For loop is not possible in Java if declaration is done in main method & initialization inside loop?

From Dev

Strong reference to self inside a method running from a block

From Dev

Trying to reference a variable from another object (JAVA)

From Dev

Remove from Array doesn't update reference inside variable?

From Dev

reference shell variable $COLUMNS from inside a bash script

From Dev

Getting local variable names defined inside a method from outside the method

From Dev

Getting local variable names defined inside a method from outside the method

From Dev

PHP reference Class from variable with static method access

From Dev

Reference a variable from another method objective-c

From Java

Java Compiler accepts generic method reference, but not when passed as variable

From Dev

pass an unsigned variable as reference in java for a method implemented in C dll

From Dev

Java reference variable declared outside method lives on stack or on heap

From Dev

How to call a java method from inside JavaScript inside fxml for javaFX

From Dev

Method reference inside synchronized expression

From Dev

Variable inside a method is not defined

Related Related

  1. 1

    Accesing private module variable from class

  2. 2

    Accesing private module variable from class

  3. 3

    Variable method reference in java 8

  4. 4

    Assigning reference returned from method to variable

  5. 5

    Java 8: Is it possible to assign a method reference to a variable?

  6. 6

    Java 8 Lambda Sort With Variable Method Reference

  7. 7

    Accesing staic variable from static function C++

  8. 8

    java8: method reference from another method reference

  9. 9

    variable scope inside of parametrized method - java generics

  10. 10

    static variable inside static method in java

  11. 11

    Accesing a variable in a ggplot function

  12. 12

    Accesing a variable in a ggplot function

  13. 13

    Accesing javascript variable in angularjs

  14. 14

    Accesing variable in nodejs module

  15. 15

    Is variable when accessed from outside the For loop is not possible in Java if declaration is done in main method & initialization inside loop?

  16. 16

    Strong reference to self inside a method running from a block

  17. 17

    Trying to reference a variable from another object (JAVA)

  18. 18

    Remove from Array doesn't update reference inside variable?

  19. 19

    reference shell variable $COLUMNS from inside a bash script

  20. 20

    Getting local variable names defined inside a method from outside the method

  21. 21

    Getting local variable names defined inside a method from outside the method

  22. 22

    PHP reference Class from variable with static method access

  23. 23

    Reference a variable from another method objective-c

  24. 24

    Java Compiler accepts generic method reference, but not when passed as variable

  25. 25

    pass an unsigned variable as reference in java for a method implemented in C dll

  26. 26

    Java reference variable declared outside method lives on stack or on heap

  27. 27

    How to call a java method from inside JavaScript inside fxml for javaFX

  28. 28

    Method reference inside synchronized expression

  29. 29

    Variable inside a method is not defined

HotTag

Archive