• 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 is the signifigance of having my return type of method or data type of variable as class name

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example I have a class 'myClass' and I create an array or a method declaration as then what is the signifigance of this?
I am currently trying to understand how ArrayList work. Here is a bit of code I wrote with help from a book. The questions are mentioned in the comments within the code.

import java.util.*;
import java.io.*;

class dvdInfo
{
String title;
String leadActor;
String genre; //can I declare them as dvdInfo title? What actually happens if I do that?

dvdInfo(String t, String l, String g)
{
title = t;
leadActor = l;
genre = g;
}

public String toString()
{
return title + " " + leadActor + " " + genre;
}
   
}

class Main
{
public static void main(String[] args)
{
ArrayList<dvdInfo> dvdlist = new ArrayList<>();
String t,l,s;
Console c = System.console();
       int i;
for(i=0; i<5; i++)
{
           t = c.readLine("Enter the title\n");
           l = c.readLine("Enter the lead Actor\n");
           s = c.readLine("Enter the genre\n");
           dvdInfo d = new dvdInfo(t, l, s);
           dvdlist.add(d); // Doesn't this imply dvdlist.add(d.toString()) ? Now, what type of object is actually getting added to the ArrayList? An object of type String or dvdInfo? I know the answer is logically dvdInfo but I
                                 //am kinda confused.
}

System.out.println(dvdlist);
}
}

Thank you for all those who help me out. Any kind of help is really appreciated.
 
Greenhorn
Posts: 25
2
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declare your dvdlist to hold elements of type dvdInfo here:


Therefore when you add an element, the object's / element's type must be dvdInfo:



Mike
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karla Carr wrote:For example I have a class 'myClass' and I create an array

Apart from the fact that is should read MyClass, that looks all right.

. . . or a method declaration as

Have you tried that sort of code? Apart from the poor formatting, did you manage to get that code past the compiler? As you will find out from the Java™ Tutorials, every method has to specify what information it returns to the outside world, and that is what you specify as the ”type” of the method. If you don't return anything, there is a keyword you use instead to denote that you aren't returning anything.

Your field will be a different type, dvdInfo, which should of course read DvdInfo. You will have to use different code to create that object.

// Doesn't this imply dvdlist.add(d.toString()) ?

No, There are a few places where toString() is called implicitly, e.g. using the + operator on Strings, or the println() method, but toString() is not called by your add() call.

Now, what type of object is actually getting added to the ArrayList? . . . I know . . . dvdInfo . . .

A dvdInfo. You have said that d (not a good name for a variable) is a dvdInfo, so that is what you are adding. You are not making any changes to d.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should know that your class is to define a type of data can be stored in your container which is the ArrayList in your codes. If you change the class name, you just changed the name of the type you defined, you will also have to change the constructor name, then nothing happens in that scope. However, if you call it out side the class, you will need to use the new name. Or the IDE will return an error saying undefined ...
reply
    Bookmark Topic Watch Topic
  • New Topic