• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the purpose of creating an object inside the class we are in the process of making?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that we instantiate things like String, ArrayList<>, or Boolean to be able to get the ability those Classes offer. Like String is good if we want to store words of some kind, ArrayList<> is good if we want to group a lot of the same type together and store it/access it like a filing cabinet. Boolean is good because it offers us the ability to compare the falseness and trueness of some statement. But why would we want to create an object of the class we are creating? what is the purpose of such an action? Cannot we get by with just using other classes functionalities?

Also is there a difference in saying TYPE vs. CLASS, I mean ArrayList<> is a type, right? like we could write a class like:



and ArrayList there would be considered a type, but it's also a class. Are these words interchangeable?


THank you
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Types include classes, interfaces, and arrays.
Yes, you can create a field which is of the same type as its surrounding object. For example the nodes in a linked list usually have references to a similar node in the next position (and often to one in the previous position). If you search for diagrams of linked lists, you find things like this (though I disagree that you are storing an array of data). Each node has a field of type node.
Why are you using a Boolean rather than a boolean?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"type" and "class" can be used interchangeably but you have to be careful of the context.  The term "type" is more general than "class" though.  You have interface types, class types, enum types, primitive types, generic types, etc, so you have to be careful not to conflate "class" with "type" if you are referring to the idea of "what something is" and that something could be "enum" or "interface" or something other than "class".

For example, if we were discussing this code:

The following statements would be technically accurate:
1. On line 1, myList is declared to be of type List
2. The class of the object assigned to myList on line 2 is ArrayList
3. The type of the object assigned to myList on line 2 is ArrayList

Whereas this is not:
On line 1, myList is declared to be an instance of the List class.

This is not accurate because List is an interface, not a class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!