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

Thread: I need help with basics.

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default I need help with basics.

    ok, so i'm guessing this is a method..

    mymethod() {
    System.out.print("Hello");
    }

    Now this doesn't run unless the method is initiated right??? How do your initiate it when you want to?? Or if i'm totally wrong how do you set a set of instructions to only run when called on..

    Please keep it simple. Thanks for the help guys!

  2. The Following User Says Thank You to Emperor_Xyn For This Useful Post:

    elTar (December 5th, 2011)


  3. #2
    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: I need help with basics.

    Please see the following for an in depth tutorial on the subject
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Methods are associated with classes, and are only run when called

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

    Emperor_Xyn (December 4th, 2011)

  5. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with basics.

    Well, I read through it and I understand them alot more... But i'm still confused how you can call one into action.. But i'm guessing it's not as easy as I thought it would be like

    run myMethod(); ect... ugh

    Or can you point me at the exact section I should re-read??? Please.

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

    Default Re: I need help with basics.

    You do not initiate methods, you call them. To do that first you need to create an object of that class. Surely you have seen examples involving Strings.
    Improving the world one idiot at a time!

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

    Emperor_Xyn (December 5th, 2011)

  8. #5
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with basics.

    Ahhh! I think I understand...

    If you don't want something to be executed you don't type public static void main(String[] args) ??? And if you do type that, everything in that class will be executed?? I think I understood it the whole time just didn't realize it. But I guess I need to improve my understanding of calling other classes.

  9. #6
    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: I need help with basics.

    If you don't want something to be executed you don't type public static void main(String[] args) ??? And if you do type that, everything in that class will be executed??
    No. main is the application entry point - you define how you want your application to flow by creating a class which contains a main method, then instantiate objects or calling other static methods from within. I highly recommend you delve into those tutorials a bit more - testing the code provided, even better changing it to see how your changes affect how the program behaves.

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

    Emperor_Xyn (December 5th, 2011)

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

    Default Re: I need help with basics.

    No!

    If you don't want something to be executed then why is it in the code? The main method is the entry point into your program. You only need one main method, however you can add a main method to every class you write for testing purposes.

    Once again if you want a method to execute it you need to call it.
    Foo f = new Foo(); //creates a Foo object
    f.doStuff(); // calls the doStuff method which now is executed
    Improving the world one idiot at a time!

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

    Emperor_Xyn (December 4th, 2011)

  13. #8
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: I need help with basics.

    Yep I think I understand =) What I meant is I don't want code not to run, just run at certain times. But thanks for the help guys, pretty sure I fully understand it now.

  14. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I need help with basics.

    Quote Originally Posted by Emperor_Xyn View Post
    Yep I think I understand =) What I meant is I don't want code not to run, just run at certain times. But thanks for the help guys, pretty sure I fully understand it now.
    Yeah you can still do this. You must read threading and more over, read the java.util.Timer for more.

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

    Emperor_Xyn (December 5th, 2011)

Similar Threads

  1. [SOLVED] Basics With UDP Packet
    By servalsoft in forum Java Networking
    Replies: 0
    Last Post: October 13th, 2011, 12:47 PM
  2. Need a little help with the basics
    By Java Neil in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 17th, 2011, 10:54 PM