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

Thread: Simple Static Method Problem

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Simple Static Method Problem

    Good evening all, I am a new member to this forum, it seems like a great place for beginners like me!

    I am at the very beginning of my Java education and I am having a little trouble with an exercise involving Static Methods.

    This exercise is designed to introduce me to invoking static methods inside a class without instantiating an object. Basically; I am just trying to print out the value of a returned calculation, but I am getting a compile error. I'll paste me code below. Thanks in advance for any guidance

    public class MyMath {

    public static long square(int a){

    long b = (long) (a*a);
    return b;
    }
    }

    public class Squares {

    public static void main(String[] args) {

    MyMath.square(12);
    System.out.println(MyMath.square());

    }
    }


  2. #2
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Simple Static Method Problem

    ...
    correct code
    Last edited by copeg; August 1st, 2013 at 03:39 PM. Reason: Removed spoofeeding

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple Static Method Problem


  4. #4
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Simple Static Method Problem

    But it was two little changes which anyone could have said in writing and the poster already had a full program. How is that spoonfeeding?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Static Method Problem

    I hope you see what llowe29 did was to combine the best parts of 2 lines of your program into one:

    These 2:
    MyMath.square(12);
    System.out.println(MyMath.square());

    Into this 1:
    System.out.println(MyMath.square(12));

    In the first line of your code, MyMath.square(12); gets the right answer but then discards it, because it isn't stored anywhere. Instead, storing the variable and using it in the print statement would also work:
    long result = MyMath.square(12);
    System.out.println( result );
    But if the variable result is not needed anywhere else, llowe29's version would be preferred.

    Similarly, the method square() could be shortened to:
        public static long square(int a)
        {
            return a * a;
        }

  6. The Following 2 Users Say Thank You to GregBrannon For This Useful Post:

    llowe29 (August 1st, 2013), nortski (August 1st, 2013)

  7. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Simple Static Method Problem

    I take it I've just been denied the help???? Thanks llowe29 for the help, I didn't get a chance to see it.

    For the moderators information, I have tried many variations of the code and Google'd the problem, but as yet was unable to find the help.

    At what point is it no longer spoon feeding?

  8. The Following User Says Thank You to nortski For This Useful Post:

    llowe29 (August 1st, 2013)

  9. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Simple Static Method Problem

    Thamks Greg for demonstrating my point and adding on.

  10. The Following User Says Thank You to llowe29 For This Useful Post:

    nortski (August 1st, 2013)

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple Static Method Problem

    Quote Originally Posted by nortski View Post
    I take it I've just been denied the help???? Thanks llowe29 for the help, I didn't get a chance to see it.

    For the moderators information, I have tried many variations of the code and Google'd the problem, but as yet was unable to find the help.

    At what point is it no longer spoon feeding?
    No, not denied, just modified. If my answer loses meaning now that llowe29's code has been removed, please ask additional questions with your updated code.

  12. #9
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Simple Static Method Problem

    It's OK, your answer makes perfect sense.

    Many thanks

  13. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple Static Method Problem

    The code was removed for the simple fact that it was a "here, your problem is now fixed" reply. In many cases this denies one the chance to problem solve - one of the most important and hardest to learn skills of programming - but also violates academic policies at many learning institutions.

  14. The Following User Says Thank You to copeg For This Useful Post:

    derekxec (August 1st, 2013)

  15. #11
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Simple Static Method Problem

    Make sure you put the SOLVED banner over your post.

Similar Threads

  1. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  2. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  3. 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
  4. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM