Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:46:15+00:00 2026-05-13T18:46:15+00:00

I am trying to add information from main() to an items class where i

  • 0

I am trying to add information from main()
to an items class where i am storing the information in a 3 different hashsets

i have 3 classes

  1. project – main()

  2. libaray – addBandMembers function

  3. Item – addband(String… member)

i am adding CD information.

first, i add band, # of songs, title – which works good

Where i am having a problem is adding band members..

I think i need to cast musicCD object to CD class then invoke the
addband function?

Im just not sure how to do that.

Here is the parts of code i think you will need to help me..

this is what i have:

Main()

item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
library.printItem(out, item);
}

Then, here the first function thats called..

This is where i need help!!!!

public void addBandMembers(Item musicCD, String... members)
{

//musicCD.addband(members);   // both cant find addband function..
 //Item.addband(members);

}

Then in another class i am trying to add the information..

 private String [] members;  


public void addband(String... member)
{
    this.members = member; 

}

oh ya, here is my set..

public class Library
{
private Set<CD> theCDs = new HashSet<CD>();
private Set<DVD> theDVDs = new HashSet<DVD>();
private Set<Book> theBooks = new HashSet<Book>();

So, from the function public void addBandMembers()
i am trying to add members to addband

is my addband function wrong?

I do have a background in C++ and i am trying to apply what i know to java so please be nice. I know i have some more reviewing to do i just cant find what i need on the web..
Thank you..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:46:16+00:00Added an answer on May 13, 2026 at 6:46 pm

    There appears to be several issues you need to address…

    First, in addBandMembers(), if it can’t find musicCD.addBand then you either need to define the addBand() method for Item or find the appropriate class that has an addBand() method based on the objects that you can access from Item.

    Second, you need to understand the difference between class methods and object methods. Class methods, identified by the “static” keyword, operate on the base class in a way that’s shared by all instantiated objects of that class. For example, static foo(x){ this.x = x; } would set the class’s static “x” variable, and any access to the “x” variable will use the last set value from calling foo() (assuming no other ways to set x). So, if you have object1 and object2, both of the class that defines foo, object1.x and object2.x would be the same location in memory, both set at the same time when calling foo(). Instance variables, identified by the distinctly missing “static” keyword, are not shared. public bar(y){ this.y = y; } would set a different location in memory for each object of the class – object1.y would be a different memory location, and a (potentially) different value than object2.y.

    Third, “Item” is a rather non-descriptive name. Is your library guaranteed to always be for music, or do you need to be more generic? Renaming the class to either LibraryItem or Media (as suggested in another answer) would clarify your code.

    Fourth, you didn’t provide nearly enough information to really diagnose what’s going on. When you ask for help, you should provide relevant output (what gets printed at the end of main?), classes where relevant variables/functions are defined (where are the addband() and addBandMembers() functions defined?) and any error messages (what error do you get when you uncomment either line within addband()?). With complete information, it’s much easier for people to help. Without complete information, it’s often impossible for people to give really good answers.

    Fifth, you talk about casting to Object but mention you don’t know how. Casting in Java is very similar to casting in C++ : Foo myFoo = (Foo)myBar;. You’ll get a ClassCastException at runtime if myBar is not a subclass of myFoo and myFoo is not a subclass of myBar. Note that you don’t need to cast subclasses to their superclasses, as the JVM already knows the class heirarchy, just like the compiler knows in C++. All classes in Java inherit from Object, so there’s almost never a need to cast to Object. On the other hand, if you happen to have a subclass of Item where addband() is defined, you can cast item (in main) to the appropriate subclass, and call the addband() method on the casted object.

     
    CompactDisk cd = (CompactDisk)item;
    cd.addband(...);
    

    or you can do it as a one-liner as

    ((CompactDisk)cd).addband(...);
    

    The first one would be useful if you need to use the object as a CompactDisk more than once. The second one would be acceptable if you only need to cast once, maybe twice both next to each other – more than that, creates readability and maintenance problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add items to listbox from another class,the information pass to the
I am trying to create a single query for getting information from the main
Trying to add a class object into a List using reflection, but when invoking
Trying to add a dynamic class value to one of my columns in CGridView:
So I'm developing a small project with Cocos2Dx but I'm trying to add Bluetooth
I'm trying to catch debug information from VMware. This might be an easy task
I'm trying to insert information from a form into a database table and then
I'm trying to simply add TimeZone information back into a LocalDate before performing some
I am parsing information from my server and have all the information being downloaded
I'm trying to add coordinate information to my database, adding django.contrib.gis support to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.