Results 1 to 5 of 5

Thread: Creating a function

  1. #1
    Join Date
    Dec 2005
    Posts
    343
    Rep Power
    0

    Default Creating a function

    Im new to java and was just trying out the language.
    Im using Net Beans and started a new project. the Main class and function was automatically created for me.
    I then tried to write some additional functions which i would call from the main class.
    No matter what I do, I am unable to call these user created functions in the main class. I did some googling for examples and tutorials and noticed that they always created the user functions in another class.

    I which case, I would have to create an instance of that class then call the function.
    Is that how it is done is Java or am I doing something wrong?
    Check out WP Spartans and Dredix

  2. #2
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    paste some code. one of the things that i noticed with java is that i had to initialize an object before i could use it.
    i did that with new

    Code:
    package helloworldapp;
    
    /**
     *
     * The HelloWorldApp class implements an application that
     * simply prints "Hello World!" to standard output.
     *
     * @author wiel
     */
    public class HelloWorldApp {
    
        /**
         * @param args the command line arguments
         */
        public void prnt(){
         System.out.println("Hello World!");
        }
        public static void main(String[] args) {
           HelloWorldApp a;
           a=new HelloWorldApp();
    
            a.prnt();// Display the string
        }
    
    }
    1337

  3. #3
    Join Date
    Dec 2005
    Posts
    343
    Rep Power
    0

    Default

    What you did was to create an instance on the class within itself.
    I must say that am a bit disappointed in Java if that is how it is done, or it just case were i have to get use to it.

    my experience is based on C#, C++ and php.
    Check out WP Spartans and Dredix

  4. #4
    Join Date
    Dec 2005
    Posts
    343
    Rep Power
    0

    Default

    By the way wiel, thanks for your help.
    Check out WP Spartans and Dredix

  5. #5
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    no problem. its not a safe practice to go around declaring global functions all the time. maybe thats why they threw that out the window
    1337

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •