Class that creates objects?

Hello everyone! Im quite new to java and oop, but im enjoying using it quite a lot. I have read some books but everything is so fresh it is easy to get confused. I think this is a very basic questions but, here it goes:
I have this simple class:
public class Soda {
int id;
int sid;
String brand;
String flavor;
}And now i want to fetch an URL that will return a text string for example "|1|23|Fanta|Orange". Now i want to create a Soda Object from this string (The String could have several Sodas)".... forget the parsing needed and the fectching for the text and everything else (Im using J2ME, and i have it solved)... but more basically:
how can i make a *method that creates Soda objects*? Funny how i get confused with the simplest things.
Thank you everyone for your comments and help!
Alejandro

AlexR wrote:
while(something) {
Soda soda=new Soda();}and each iteration of the while loop will create a different object? or overwrite over and over the created soda object?Each iteraion will create a new Soda object, and overwrite the soda variable with a reference to that object. If you want to have access to all the Soda objects after the loop exits, you need to add references to them to a collection or array.
[http://java.sun.com/docs/books/tutorial/collections/]
[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
Also, you can create constructors that take arguments.
String brand = (parse brand from string);
String flavor = (parse flavor from string);
Soda soda = new Soda(brand, flavor);
public class Soda {
  private final String brand;
  private final String flavor;
  public Soda(String brand, String flavor) {
    this.brand = brand;
    this.flavor = flavor;
}

Similar Messages

  • Referencing a class that created another class

    Is there a certain way reference the class that created "you" - examples:
    public class SomeClass
      public SomeClass()
        // blah blah
        SomeOtherClass foo = new SomeOtherClass();
    public class SomeOtherClass
      public SomeOtherClass()
        // blah
      public void SomeMethod()
        // access SomeClass here
        TheClassThatCreatedMe.method();
    }Any ideas?

    one way is u can modify ur code this way
    public class SomeClass{  public SomeClass()  {    // blah blah    SomeOtherClass foo = new SomeOtherClass(String creator);  }}
    //where string creator u pass the same clasas name i.e someclass
    public class SomeOtherClass{  public SomeOtherClass()  {
    System.out.println("created class name is "+creator );
    // blah
    } public void SomeMethod() {    // access SomeClass here    TheClassThatCreatedMe.method();  }}

  • Class that creates classes on the fly?

    Hello,
    how do I do this:
    I want to have a class that produces special .class-files (with different variables, different methods) on the fly (during production).
    It should create those files so that they are ready to call (from any other class).
    Is this possible?
    What would be the best solution to this problem?
    Greetings
    zuhans

    Should you really need it:
    http://jakarta.apache.org/bcel/
    The Byte Code Engineering Library is intended to give users a convenient possibility to analyze, create, and manipulate (binary) Java class files (those ending with .class). Classes are represented by objects which contain all the symbolic information of the given class: methods, fields and byte code instructions, in particular.
    Such objects can be read from an existing file, be transformed by a program (e.g. a class loader at run-time) and dumped to a file again. An even more interesting application is the creation of classes from scratch at run-time.

  • I need to know the Object that created my Object

    help needed!!
    From inside a method of myclass, I want to call a method of the class that created myclass..
    public class BSQL{
    private Manager m;
    public void BSQL() {
    m = new Manager();
    public void beeep() {...}
    The other class:
    public class Manager {
    public void x{
    !!need to call beeep() from the BSQL that created me

    Try this....
    public class BSQL {
         private Manager man;
         public BSQL() {
              man = new Manager(this);
              man.doManagerStuff();
         public void doSqlStuff() {
              System.out.println("Doing stuff.");
         public static void main(String args[]) {
              BSQL b = new BSQL();
    public class Manager {
         private BSQL creator;
         public Manager(BSQL creator) {
              this.creator = creator;
         public void doManagerStuff() {
              creator.doSqlStuff();
    }

  • Problem Create object from superclass

    Hi,
    I have a problem with classes:
    I have one abstract superclass which has a method to create objecto for its children class.
    CREATE OBJECT R_FLIGHT TYPE (l_class_name)
         EXPORTING
           i_sflight = l_sflight.
    l_class_name: has the name of my children class
    My children class has this properties:
    A dump its triggered when I try to create object from the superclass.
    Best Regards

    Hello,
    I think that you defined the instantiation level of the superclass as PROTECTED & from the screenshot i can see that the instantiation level is protected & the class is marked as "Final".
    Are you having a factory method which returns the instance of the abstract class & you use it to achieve polymorphism?
    May be a little bit insight into your coding can help us
    BR,
    Suhas

  • Creating objects in a second instance

    I have created a second instance through the console of a directory server, which has been configured with the same rootdn as the original.
    When I open up the console why in the new instance does it show the rootdn on the config tab but the original doesnt ......
    also ....
    why when i go into the second instance i try creating data objects for root suffixes ive created to mirror the original instance, does it create them under cn=config ......
    Thanks
    G

    Hi
    I have read that creating objects in a loop degrades
    performance. Can someone explain why this should be
    so?Every object creation takes time, so if you create
    lot of unnecessary temporary items in loop it degrades
    performance (so it is nothing loop specific).

  • Creating objects in a loop

    Hi
    I have read that creating objects in a loop degrades performance. Can someone explain why this should be so?

    Hi
    I have read that creating objects in a loop degrades
    performance. Can someone explain why this should be
    so?Every object creation takes time, so if you create
    lot of unnecessary temporary items in loop it degrades
    performance (so it is nothing loop specific).

  • How to force explicit schema for created objects?

    Is it possible to force create statements to require an explicit schema?
    When we deploy new applications, we run scripts to create tables and such in appropriate the appropriate schema. Since we always connect as ourselves, I want to minimize the possibility that I forget to put the schema and accidentally create the object in my own schema. I really don't want to have to connect as a different user, or rely on changing the current schema in login.sql or a logon trigger.
    Thanks,
    Matt Knowles

    Another option would be a DDL trigger in your schema that would raise a more targeted exception if you attempt to create a table there. Obviously, that is easier to bypass, but if the concern is just that someone accidentally creates objects in the wrong schema rather than someone deciding that creating objects in their schema is something they want to do, that's probably not a concern. And if there are multiple people that could do the deployment, a DDL trigger's error message may be less likely to be confused with a quota-related error that could also indicate a problem with the script.
    Justin
    Edited by: Justin Cave on Jan 18, 2011 10:48 AM
    I didn't see your last update before posting. I suppose a DDL trigger could parse the DDL statement itself looking for the schema qualifier and only throw an exception if the schema qualifier was missing. Not completely foolproof, of course, but parsing most DDL shouldn't be terribly difficult.

  • Making a generic class that uses interfaces.

    I've learned how to do this:
    class MyClass<T extends Object> {
    }But I want a generic class that takes objects that implement another object like this:
    class MyClass<T implements Runnable> {
    }Why doesn't this work? Is there any way to do this?

    happypigface wrote:
    Yes thank you. Thats strange that Java does that. I didn't really test it yet, but I know Runnable is an instance and eclipse doesn't have any compiling errors for it. I'm glad that you can do this. It wouldn't very useful if you could only specify that the generic class had to be a subclass, not an implementation of a class.Generics are about types rather than classes and interfaces. Hence, one specifies the supertype(s) of a Generic Parameter by using the extends keyword.
    Note, however, it's possible to extend multiple interface types but only one class type.

  • Create  object for a class in jar file

    Hi
    I am using Oracle JDeveloper 10g. I have created a main application using Swing/JClient technologies. I am facing a problem for the past one week. I let u all people know my problem and I kindly request you to send a solution if known possibly confirmed.
    Problem: I have created a main application using swing that class extends JFrame. This main application consists of two parts. One left side panel and one right side panel. Left side panel contains JTree component. Each item in that tree refers to a class file located in a separate jar file.i.e that acts as a separate application. If we are executing separately that jar file class, I am able to see that small application running. If I am selecting an item in JTree during runtime, I should place that small application from the respective jar file in the right side panel of the main application. I can locate the jar file and even I can create object to that particular class file. But I cannot execute the application even separately and also not able to place in the right side panel of the main application.
    Note: The small application from the jar file contains one class file containing main method. That class file extends JPanel and implements JUPanel. i.e., with data binding. I am trying to create an object for this class file with data binding from my main application. Navigation bar is used in the main class file.
    If possible can any provide me solution at the earliest. Waiting eagerly for the positive reply.
    Regards,
    s.senthilkumar

    Can you sure that there is only a A class in the classpath of Tomcat, I advise you to add
    some statements into the A class so that you can be sure which classloader is used
    to load the A class.

  • How to create object by getting class name as input

    hi
    i need to create object for the existing classes by getting class name as input from the user at run time.
    how can i do it.
    for exapmle ExpnEvaluation is a class, i will get this class name as input and store it in a string
    String classname = "ExpnEvaluation";
    now how can i create object for the class ExpnEvaluation by using the string classname in which the class name is storted
    Thanks in advance

    i think we have to cast it, can u help me how to cast
    the obj to the classname that i get as inputThat's exactly the point why you shouldn't be doing this. You can't have a dynamic cast at compile time already. It doesn't make sense. Which is also the reason why class.forName().newInstance() is a very nice but also often very useless tool, unless the classes loaded all share a mutual interface.

  • How to create a container class for 2 object?

    I use JDK to create 2 objects, one is Customer and one is Book. I need to enqueue these 2 objects, but they canot share the same queue class. Some one told me that I can create a container class for these 2 objects but I don't know how to create it. Can some one tell me how to create the container class?

    I use JDK to create 2 objects, one is Customer and one
    is Book. I need to enqueue these 2 objects, but they
    canot share the same queue class. Some one told me
    that I can create a container class for these 2
    objects but I don't know how to create it. Can some
    one tell me how to create the container class?
    class CustomerBook{
    Book m_book;
    Customer m_customer;
    pulbic CustomerBook (Customer customer, Book book){
    m_book = book;
    m_customer = customer;
    }If you want to create a class that represents one customer and many books, do this:
    class CustomerBooks{
    Vector m_books;
    Customer m_customer;
    pulbic CustomerBook (Customer customer){
    m_books = new Vector();
    m_customer = customer;
    public void addBook (Book book){
    m_books.addElement (book);
    public void displayBooks (){
    //I assume the Book class has a toString method or something similar
    for (int i = 0;i < m_books.size();i++){
      System.out.println ("book: "+((Book)m_books.elementAt(i)).toString());

  • Creating object class dynamically

    Hi ,
    I need to create object dynamically for a class.eg when i loop thru internal table i need to create an object per pass of the loop and no of entries  in itab can be known at runtime only
    Any clues
    Regards
    Swatantra

    Check this sample program, where it is creating a number of objects and appending them to a object list. 
    report zrich_0001.
    types: begin of telements,
           element(30) type c,
           value(30) type c,
           end of telements.
    *       CLASS lcl_element DEFINITION
    class lcl_element definition.
      public section.
        data: ielements type table of telements,
              xelements type telements.
        methods: constructor importing im_elements type telements,
                 write_out.
    endclass.
    *       CLASS lcl_element IMPLEMENTATION
    class lcl_element implementation.
      method constructor.
        append im_elements to ielements.
      endmethod.
      method write_out.
        loop at ielements into xelements.
          write:/ xelements-element, xelements-value.
        endloop.
      endmethod.
    endclass.
    data: itab type table of telements with header line.
    data: r_element type ref to lcl_element.
    data: r_elementlist type table of ref to lcl_element.
    start-of-selection.
    * Create the initial list of elements.
      itab-element = 'ELEMENTA'.
      itab-value   = 'Val_A'.
      append itab.
      itab-element = 'ELEMENTB'.
      itab-value   = 'Val_B'.
      append itab.
      itab-element = 'ELEMENTC'.
      itab-value   = 'Val_C'.
      append itab.
    * Create instances of the element object
    * and add to the list
      loop at itab .
        create object r_element
            exporting
                 im_elements = itab.
        append r_element to r_elementlist.
      endloop.
    * loop at the list and write out the element objects.
    * Notice that the r_elementlist is an internal table
    * which contains 3 objects.  Those three objects each
    * contain 1 internal table with one record each.
      loop at r_elementlist into r_element.
        call method r_element->write_out.
      endloop.
    Regards,
    Rich Heilman

  • Re: How to create More two class with one object

    haii,
             i have small information How to create More two class with one object,
    bye
    bye
    babu

    Hello
    I assume you want to create multiple instance of your class.
    Assuming that you class is NOT a singleton then simply repeat the CREATE OBJECT statement as many times as you need.
    TYPES: begin of ty_s_class.
    TYPES: instance   TYPE REF TO zcl_myclass.
    TYPES: end of ty_s_class.
    DATA:
      lt_itab      TYPE STANDARD TABLE OF ty_s_class
                     WITH DEFAULT KEY,
      ls_record  TYPE ty_s_class.
      DO 10 TIMES.
        CLEAR: ls_record-instance.
        CREATE OBJECT ls_record-instance.
        APPEND ls_record TO lt_itab.
      ENDDO.
    Regards
      Uwe

  • Adding object classes when creating ldap user in workflow

    I'm creating ldap users in a workflow and when I assign the object classes in the workflow I get an object class violation. It seems that when I call check in view and when my break point stops in Update User the default object classes on the resource have been removed from the user.accounts[LDAP].objectClass attribute which I just set. Not sure what's going on here. Is there another way to assign more than just the default object classes to a new ldap user through the workflow? Thanks in advance.

    Multiple things I can think of
    1) put all the object classes you may be expecting with the user account in the resource configuration panel. LDAP is smart enough to assign the related object classes to the object based on the attributes assigned to the user.
    2) Check if you have the object class in the schema of LDAP.

Maybe you are looking for