Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Fill in Container Class using another Class ?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Fill in Container Class using another Class ?

    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(...);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM