Results 1 to 6 of 6
  1. #1
    Fedor is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default create new instance of variable class

    Ok,

    This question might be silly to Java programmers:

    in PHP you can do

    $MyObject= new $ClassName(arg1,arg2[,...]);

    I use this a lot when parsing scripts to create objects. Is there a way to do this with Java?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    perhaps you want to look in the Reflection Tutorial.

  3. #3
    Fedor is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default I did

    I already did. What I did learn is how to set and retrieve properties but I couldn't figure out how to create a new instance...

    Reflection might very well be the right direction but maybe someone can point out te me what I was overlooking in there...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps it would be better for you to describe your ultimate goal rather than this specific step towards your goal. I know that Java and PHP have vastly different philosophies, and what works well in one language often doesn't work well in the other. Please let us know the overall problem you are trying to solve, and perhaps we can find you a better way to solve it from a Java point of view.

    Best of luck.

  5. #5
    Fedor is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default Ok, let's try that

    Ok, let's try it that way. What I'm trying to do is this:

    The application (applet actually) reads a script that describes a customer from IT support perspective. It reads elements that will be represented by objects of different classes. Some o those classes are containers, others are end-nodes. examples are:
    - building
    - floor/level
    - office
    - meetingroom
    - copier
    - computer
    - printer

    etc...

    So my script could be:

    Java Code:
    BUILDING:Mainroad6
      FLOOR:Ground Floor
         OFFICE:0.01
            COMPUTER:FG-PC-0005
            COMPUTER:FG-PC-0006
            PRINTER:FG-PR-0100
         MEETINGROOM:0.03
            BEAMER:FG-BM-0034
    etc...
    The application reads the script and creates the objects that are mentioned in the script.

    I expect to have about 80 different objects, so I was hoping to do this whithour switching of if's...

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    To create an instance of a class without using the customary 'new' operator, you use Class#newInstance() or Constructor#newInstance. Read the API for those methods.

    db

Similar Threads

  1. Array of objects, as an Instance variable
    By blaklite in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 01:56 AM
  2. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 05:26 PM
  3. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 07:27 PM
  4. Instance Variable In Servlet
    By javarishi in forum Java Servlet
    Replies: 3
    Last Post: 06-14-2008, 09:28 AM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •