Fill in Container Class using another Class ?
hi there,
In order to Complete a task sheet for College, i have to create a media Library
filled with information such as : First Name, Last Name, Artist, Audio length, etc. (just the info).
i thought i would create a Container Class in which I'd fill in all the info, using another Class (Information Class)
this is where i am right now:
Code :
import java.util.ArrayList;
public class Container {
public static void main(String[] args) {
}
ArrayList<String> Names = new ArrayList<>();
public void addinfo(String addinfo) {
Names.add(addinfo);
}
}
but i don't know how to grab the method into the other class and use it from there to fill it.
we are allowed to use java.util and java.lang, prompted to use the ArrayList for the Container.
PS: Feedback on my way of handling the task would be Highly Appreciated.
Thanks in advance
Re: Fill in Container Class using another Class ?
Container is the name of a class in java SE. You should use a different name for your class.
Naming conventions for variables recommends lowercase first letter:
names vs Names
Quote:
how to grab the method into the other class and use it
Get a reference to an instance of the class and use it to call the class's methods.
Just like names is a reference to an instance of the ArrayList class and add() is a method in that class that the code calls.
Re: Fill in Container Class using another Class ?
Yeah, but what i don't get
in the second Class (Information) now:
public class Information {
Containment addinfo = new Containment();
}
how do i get the method which i have used in the Containment(new name i gave) class?
i want the addinfo (so i can use it's function)
but just don't know how you call to use it?
Setters and Getters ?
Re: Fill in Container Class using another Class ?
Quote:
how do i get the method
What is the name of the method and what class is it defined in?
If addInfo() is a public method in the Containment class you can call it the same way you called the ArrayList's add() method in post#1: Use the reference to an instance (names) of the class: names.add(...);