Results 1 to 5 of 5
  1. #1
    ischuldt is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default multi-line construction.

    So it's been awhile for me on java, I've been stuck in c++ for awhile, but i'm trying to remember how java would naturally solve this.

    so lets say i have class called City with a getter that returns the name of the city in string form.

    I can initialize that by saying

    Java Code:
    City my_city = new City("New York");
    system.out(my_city.name());
    If I remember correctly though I can also do it this way..

    Java Code:
    City my_city;
    my_city = new City("New York");
    system.out(my_city.name());
    but what would happen if I did this...?

    Java Code:
    City my_city;
    system.out(my_city.name());
    would an error get thrown or would it just print a blank string?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A null pointer exception will get thrown when you attempt to call a method on an uninitialized object reference.

  3. #3
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    You'd get an error because my_city has not been initialized.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  4. #4
    ischuldt is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    ok so what if I did this...

    Java Code:
    State my_state;
    //my_state = new State("NY");
    
    City my_city;
    my_city = new City(my_state, "New York");
    can I assume the initialization of the new City would also throw that error since state isn't initialized?

    Also would that be a run time or a compile time error?

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  2. Multi Valued Command Line arguments
    By joey86 in forum Advanced Java
    Replies: 9
    Last Post: 01-08-2010, 12:07 AM
  3. AVL-tree construction
    By student89 in forum Advanced Java
    Replies: 0
    Last Post: 10-27-2008, 05:33 AM
  4. Multi-line Tooltip inside a JTable's Cell
    By stevenc49 in forum AWT / Swing
    Replies: 2
    Last Post: 06-29-2008, 04:15 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
  •