• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79036
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic