-1

Instance of package1.MyClass is an object that has identity, properties, behavior and holds association(composition/aggregation) with other instances(objects).

Say, package1.MySubClass extends package1.MyClass


Below code uses Class meta programming abstraction,

Class<?> myClassInstance = Class.forName("package1.MyClass");

that provides information about package1.MyClass class.


1) Is package1.MyClass an object?

2) If yes, Does package1.MyClass object has it's own identity, properties, behavior and holds information about generalization relation with package1.MySubClass object?

3) Why does Java call meta-programming as Reflection?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • It's an object, not clear exactly what 2 is, and 3 is mostly because it's not metaprogramming. You want to take a look at [ask] and [help/on-topic], though. – pvg Sep 16 '17 at 11:08
  • @pvg Does question 2 make sense now? – overexchange Sep 16 '17 at 11:14
  • @pvg [here](https://stackoverflow.com/a/7670562/3317808) it says, reflection is subset of metaprogramming – overexchange Sep 16 '17 at 11:17
  • No. I mean, it's 3 different question, the first one and last one seem trivial and the second one is kind of incomprehensible although I kind of want to say 'yes of course it is an object with its own identity and yes of course it's not really a generalization of MySubClass, it's a parametrization of Class' – pvg Sep 16 '17 at 11:18
  • 1
    It doesn't say that. It says something along the lines of reflection being a facility useful for certain kinds of metaprogramming implementations. Java does not call 'metaprogramming' reflection because reflection doesn't mean 'metaprogramming', it means something else. Systems like C++ metaprogramming or lisp macros don't strictly depend on runtime reflection. – pvg Sep 16 '17 at 11:23
  • Anyway, I'm flagging this as 'too broad' since it's a whole bunch of stuff, doesn't seem to show any substantial independent research effort and kind of reads like someone's essay homework. – pvg Sep 16 '17 at 11:24
  • Note that syntactically, `package1.MyClass` refers to a *type*, not an object, and is a syntax error when used in a context where an object is required. On the other hand, `package1.MyClass.class` is the syntax to refer to the corresponding `Class` object. While they are conceptually the same thing, syntactically there is a difference. – Daniel Pryden Sep 16 '17 at 11:25
  • #1) Do you mean the return value from `forName`? I mean, anything but primitives are objects in Java, so... #2)there are docs and debuggers to tell you what capabilities/properties/etc objects and classes have... #3) it doesn't, "reflection" just means you can poke around programmatically, which is a subset of meta-programming. – Dave Newton Sep 16 '17 at 11:25
  • @DaveNewton `x.class` is a property of `package1.MyClass` object. Isn't name `package1.MyClass` an **object** of `class` type? If primitive types(`int`/`char`) were extending `java.lang.Object` then I would *everything is an object* in Java – overexchange Sep 16 '17 at 11:29
  • 1
    @overexchange And I'm saying everything that isn't a primitive (short list of primitives) in Java is an object, so if you're referring to something that isn't a primitive, it's an object. You can see exactly what it is by looking at the docs or inspecting it with a debugger. – Dave Newton Sep 16 '17 at 11:33
  • I agree that this question is poorly researched. It is (in part) based on false assumptions and incorrect reading of other sources; i.e. the mistaken assertion that reflection is a kind of metaprogramming. For the rest, those are basic questions that can easily be answered by reading *any one* of the Oracle Java tutorial, the javadocs or the JLS. It *could be* updated to address these flaws ... but hasn't been (yet). – Stephen C Sep 16 '17 at 12:20

1 Answers1

1

The javadoc for Class shows it derives from Object, hence it is an object.You can call toString(), hashCode() etc. on a Class object. In addition you can call Class-specific methods to identify specific properties. See the javadoc for everything available.

Note that reflection is not a Java-specific term. The term meta-programming isn't widely used in the Java world.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Term or capability? [here](https://stackoverflow.com/a/7670562/3317808), it says reflection is subset of meta programming, in a language agnostic way – overexchange Sep 16 '17 at 11:22
  • I didn't downvote, but I'd disagree that the term isn't used in Java. I'd say using the term isn't as widespread as in languages that make it easier. – Dave Newton Sep 16 '17 at 11:30
  • I downvoted it, as an answer, it's lazy and vaguely misleading/inaccurate/incomplete. More importantly, the question itself is poor enough to be off-topic, one's efforts are better directed at helping the poster to narrow it down and/or reformulate rather than rushing to provide a similarly poor answer. – pvg Sep 16 '17 at 12:07
  • Well I disagree (and would appreciate a comment alongside the downvote). It specifically answers points 1 and 2, and I would suggest that the phrase meta-programming is not widely used in the Java community. Most conversations I have revolve around the term (and API) reflection – Brian Agnew Sep 16 '17 at 12:13
  • You're not really owed comments on downvotes so I find that an odd thing to complain about but ok. I think the javadoc argument seems non-fundamental and backwards. The question isn't whether `Class` is a subtype of the type `Object`. 2 is not addressed at all and 3 is vague and kind of wrong, and again, doesn't address the question posed in 3 itself. So it's a bad answer to a bad question and does little to attempt to improve the bad question, which I think is the bigger sin. However well-intentioned, that just makes the site worse, in my mind. – pvg Sep 16 '17 at 12:41
  • 'that just makes the site worse, in my mind' - in that case editing the answer or feedback would be a valuable contribution. That's how I've seen this site grow (and helped it grow, note) – Brian Agnew Sep 16 '17 at 13:08