1

I have the following class:

public class Example {

    private String name;
    private int year;

    public Example() {

        name = "Clifford the big red dog";
        year = 2020;
    }

    public void showData() {
        // ?????
    }
}

Imagine in the main class I create an object of the previous class (Example) and execute it's constructor. Then I want to execute showData() and I would like to see the following output (given this example):

String name Clifford the big red dog
int year 2020

The main would look like this:

public static void main () {

    Example example = new Example();

    example.showData();
}

Is there any way I can do this?

7
  • 2
    Yes, with reflection. Commented Mar 25, 2020 at 18:15
  • @Kayaman could you give an example please? Commented Mar 25, 2020 at 18:17
  • 1
    Reflection is a bit advanced, and it's not a general purpose tool. Is there a reason you want to print out the variable types, names and values? Is it for debugging purposes? Commented Mar 25, 2020 at 18:20
  • Not for debugging, I'm connecting remotely to my SQL database manager and it would help on the creation of tables through the Java program Commented Mar 25, 2020 at 18:30
  • Because I was wondering if I could create a function that could work on any class so I can create as many tables as I need on the SQL database without typing each variable name manually Commented Mar 25, 2020 at 18:34

1 Answer 1

1

Do it as follows:

class Example {

    private String name;
    private int year;

    public Example() {

        name = "Clifford the big red dog";
        year = 2020;
    }

    public void showData() {
        System.out.println(name.getClass().getSimpleName() + " name " + name);
        System.out.println(((Object) year).getClass().getSimpleName() + " year " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        Example example = new Example();
        example.showData();
    }
}

Output:

String name Clifford the big red dog
Integer year 2020

However, since showData is a method of class Example which already knows about its instance variables, you can simply do it as:

class Example {

    private String name;
    private int year;

    public Example() {

        name = "Clifford the big red dog";
        year = 2020;
    }

    public void showData() {
        System.out.println("String name " + name);
        System.out.println("int year " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        Example example = new Example();
        example.showData();
    }
}

Output:

String name Clifford the big red dog
int year 2020
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer! But if you didn't know how many variables the class had, would still exist a way to do this? (using a loop or something)?
Thanks, @user85421 for the valuable feedback. I've updated my answer to incorporate it.
@user157629 - You can get the list of instance variables using reflection. Check stackoverflow.com/questions/2989560/…
Thanks! I will give it a look :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.