• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java class used as datatype in the same class itself  RSS feed

 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// First off I want to thank you for taking you time and Im hoping you would understand my question because Im pretty bad at explaining stuff so please hang on. lol


//and the code below is a different code for another class.


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobbyy Leee wrote:

'next' is a reference that refers to an object of type 'Link'. It doesn't matter if 'next' is of the same class as the class it resides in. This is how you make linked lists.

Bobbyy Leee wrote:

This method returns a reference referring to an object of type Person. We can only assume that 'a' is an array of Person references seeing as how the method is returning a[j].
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bobbyy, welcome to the ranch.


// (can someone explain what reference to the Link class itself does or work?
It is perfectly valid for a class to have an attribute with the datatype being its own class.  In fact it is not uncommon to see at all.
In this case it is an attribute just like any other.  It has a data type (being a Class called "Link")

In this case it lets one "link" in your list refer to another (presumably different) link.


// what about this method? Its not using any dataType but using a class Person. how does this work? I understand what the code does btw.
In order for this code to compile there has to be a class Person declared somewhere for you to use.
Also the variables/attributes 'a' (which looks to be an array of Person objects) and 'nElems' need to be declared and available.


P.S. When posting codes please UseCodeTags to make it easier on us.  There is a handy "code" button on the interface for you to use.  I've added them for you this time.
 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stefan and Carey
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:It is perfectly valid for a class to have an attribute with the datatype being its own class.

The problem is how do you initialize next? Any attempt to explicitly initialize next will cause java.lang.StackOverflowError



 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Link as defined in the OP:

etc etc.

A null 'next' simply means you have reached the end of the Links.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Link class (which shou‍ld probably really be called Node) forms part of a linked list, as you can read about in this Wikipedia link: Linked_list. That shou‍ld explain everything.
The second method is a kind of linear search; it looks for the first Person whose last name matches the input. It is pretty awful code; I hope you didn't write it yourself. Please tell us where such code comes from.

And welcome again
 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, thanks for answering and taking your valuable time. So to my question, when I create a Link object in the main method, it basically allow to the object to access next?

The second question I asked is from Data Structures and Algorithms in Java (2nd Edition) by Robert Lafore (chapter 2(listing 2.5)). About the question, when I call this method, I use the current Object I created to find the Person named by LastN?

Hope you guys understand what I'm saying. Thanks again.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Lee wrote:Hey all, thanks for answering and taking your valuable time. So to my question, when I create a Link object in the main method, it basically allow to the object to access next?
Mostly, yes. It also allows you to access the payload, which in your original code is "long dData".

Bobby Lee wrote:The second question I asked is from Data Structures and Algorithms in Java (2nd Edition) by Robert Lafore (chapter 2(listing 2.5)). About the question, when I call this method, I use the current Object I created to find the Person named by LastN?

I don't have a copy of the book so I can't say.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea behind a linked list is you have a bunch of things, and each one can tell you who the next one in the list is. So each object has a reference to "the next guy in line". 

Much of the time, you'll have a reference to "the first guy in line" somewhere. So when you want to go through the list, you start with the first guy, do whatever you need to do to/with him, then you say "who is next?"

He will either say "that guy is next", or he will say "there's nobody after me". 
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation, Fred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!