Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Junky
  • 1 Post By JeffGrigg

Thread: Create an object without calling the constructor?

  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Create an object without calling the constructor?

    Pretty much what the title says. I am wanting to know how to create an object without calling the class' constructor.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Why would you want to do that?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Why? What are you trying to do?

    I'm not sure that such a thing is possible. Even Constructor<T>'s newInstance() "uses the constructor represented by...". It is the job of constructors to construct, and other code relies on objects having been constructed properly.

  4. #4
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default

    The constructor opens a window and I dont want the window to open twice but I still need to use the methods in that class. If there is a better way please tell me.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What?

    Constructors do not open windows unless you have added code to do that. If it it is not doing what you want then you need to fix the code so it does do what you want.
    JeffGrigg likes this.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Move the utility methods to a separate class from the GUI class.

  7. #7
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    It is quite impossible to create an instance without calling a constructor.

    However, a class can have as many constructor methods as you want. This might help you accomplish what you want.
    Tolls likes this.

  8. #8
    dmheli is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Create an object without calling the constructor?

    overload the constructor and use that new constructor. This way you can omit the "open a window" while maintaining the 1st constructor that opens a window, and at the same time you can still access the class' methods.

Similar Threads

  1. constructor calling sequence.
    By himanshu19 in forum New To Java
    Replies: 1
    Last Post: 02-04-2010, 02:40 PM
  2. Calling a constructor from another one.
    By Somelauw in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 03:42 PM
  3. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 06:53 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 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
  •