Java: Reference vs. Object
Declaring and initializing reference variables
When learning Java we see reference variables declared and initialized like this:
- Case A: ABC varName = new ABC();
Later in our learning, we see something like this:
- Case B: ABC varName = new DEF();
Let's take this as an example for discussion:
- Dthing varName = new Othing();
What is Dthing and what is Othing?
From the first chapters of introduction to Java we know that Dthing is the type, and Othing is a constructor, and so is the class of varName.
More advanced instruction shows:
From the first chapters of introduction to Java we know that Dthing is the type, and Othing is a constructor, and so is the class of varName.
More advanced instruction shows:
- Dthing is the Declared Type, or Reference Type (I have found them used interchangeably)
- Othing is the Object Type
This vocabulary is needed for reasons including discussion about when to declare/instantiate with the Declared Type being the same as the Object type, and when they should be different.
An example of that decision would be in the below. The why is another subject/post:
So remember that Declared Type and Object Type may be the same, but might not be.
References, Internal:
Below is screenshot and GitHub of a small project that reveals a bit about the declarations and references.
https://github.com/khervey/classSubclassDemo
Reference, External
Creating Objects
java - Reference type and object type - Stack Overflow
~~
An example of that decision would be in the below. The why is another subject/post:
- List<String> varString = new ArrayList<String>();
- ArrayList<String> varString = new ArrayList<String>()
Also, for future topic: DeclaredType versus TypeElement
So remember that Declared Type and Object Type may be the same, but might not be.
References, Internal:
Below is screenshot and GitHub of a small project that reveals a bit about the declarations and references.
https://github.com/khervey/classSubclassDemo
- Note: Visit the GitHub at the above link to see any updates
Reference, External
Creating Objects
java - Reference type and object type - Stack Overflow
~~
Comments
Post a Comment