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 6 of 6

Thread: static method in super class

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Superior, CO 80027
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default static method in super class

    Suppose class A has many subclasses B, C, D,... (the names can be very irregular).
    Is there a way to implement a mathod

    public static newInstance(String xml)

    in class A such that when I say
    B.newInstance(anXml), the method in A will be invoked and returns an object of type B,
    while C.newInstance(aValue) will invoke the same method but returns an object of type C?

    Thanks for your help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: static method in super class

    Have you tried writing a simple test case and see what happens?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Superior, CO 80027
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: static method in super class

    Quote Originally Posted by Norm View Post
    Have you tried writing a simple test case and see what happens?
    No. I don't know how. I used to program with ObjectC (cocoa), it's not hard doing such thing with
    ObjectC. And I'd like to port code to Java

    I heard that I need reflection functionalities in Java to do that.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: static method in super class

    Why are you trying to use a static method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    Superior, CO 80027
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: static method in super class

    Quote Originally Posted by Norm View Post
    Why are you trying to use a static method?
    Because I don't assume an object of type B (C, D,...) exists. I think any method creating objects is static...

    The reason I'd like to do things that way is that if I put newInstance method in each subclass, they look very similar and yet it's a big method, the resulting code will be very hard to maintain, and also ugly.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: static method in super class

    I think any method creating objects is static...
    You should do some research on static in java. Here's a page in the tutorial.
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Instances of a class are created with the new statement.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with static method as parameter of super()
    By Cento e Cem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 28th, 2012, 01:11 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM