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?

user157629
  • 624
  • 4
  • 17
  • 2
    Yes, with [reflection](https://docs.oracle.com/javase/tutorial/reflect/index.html). – Kayaman Mar 25 '20 at 18:15
  • @Kayaman could you give an example please? – user157629 Mar 25 '20 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? – Kayaman Mar 25 '20 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 – user157629 Mar 25 '20 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 – user157629 Mar 25 '20 at 18:34
  • So you want to create a table programmatically based on a class? You can get the fields with `Class.getDeclaredFields();`, so in your case `Example.class.getDeclaredFields();`, it returns `Field[]` containing names, types and other info. – Kayaman Mar 25 '20 at 18:38
  • I created this class as an example but yeah, `showData()` shouldn't be inside the class for what I know. Thanks for the comments, I'll start trying this with `getDeclaredFields();` as you guys say. – user157629 Mar 25 '20 at 18:44

1 Answers1

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110