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

Thread: Confused

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Confused

    I don't understand why this won't work? thank you.

     
    public void duck(){
    		String yellow = "eric";
    		System.out.println("" + yellow);
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused

    What exactly do you mean when you say that this won't work?

    Where is your class? Where is your main method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    public class Wamidh {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    	}
    public void duck(){
    		String yellow = "eric";
    		System.out.println("" + yellow);
    	}

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused

    Okay, and what exactly do you mean when you say this doesn't work? When do you call your duck() function?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    it won't say eric when I click run

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused

    Why would it? When do you call your duck() function?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    wamidh (September 17th, 2014)

  8. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    I am meant to call my function? how do I do this?

    thank you for your reply.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Confused

    Yes, you have to call your function. Java won't automatically call your function for you.

    However, Java *does* automatically call the main() method for you. That's the whole point of the main() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    oh, thank you but what do I write to call my function duck?

  11. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused

    Have you tried:

    public class Wamidh {
      public static void main(String[] args) {
        duck(); //<---
      }
     
      public void duck(){
        String yellow = "eric";
        System.out.println("" + yellow);
      }
    }

  12. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    I tried that out but then it said cannot make static reference to non-static method duck() from the type Wamidh.
    Then I changed the duck function to static and then it worked!
    I don't know if it's right but at least it works now.
    public class Wamidh {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    	duck();
     
    	}
     
    	public static void duck(){
    		String yellow = "eric";
    		System.out.println("" + yellow);
    	}

  13. #12
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused

    In all fairness, all i did was copy your 2nd post and replace // TODO Auto-generated method stub with duck();
    I didn't even look at anything else. Good on you figuring out static. =)

  14. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Confused

    Be careful. Just sticking static in everywhere is not a good solution and goes against OO principals.
    Improving the world one idiot at a time!

  15. The Following User Says Thank You to Junky For This Useful Post:

    KevinWorkman (September 18th, 2014)

  16. #14
    Junior Member
    Join Date
    Sep 2014
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Confused

    okay, thank you.

Similar Threads

  1. So confused, I need help!
    By sessypeanut in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 10:45 PM
  2. I'm confused
    By Wonderlandslost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2012, 01:33 PM
  3. help me , im confused!!!!
    By sephskie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 10:52 AM
  4. [SOLVED] confused
    By joon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 12th, 2011, 11:40 AM
  5. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM