File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Very beginner question: calling methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very beginner question: calling methods" Watch "Very beginner question: calling methods" New topic
Author

Very beginner question: calling methods

jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7




in the above code, i have two methods, main and jay within the hiya class.
the main method runs automatically, but how do i call the method jay to make that run?
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
additional question on the above code:

System.out.Println is a method within the Java API

are System and out super classes that are being referenced by Println ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

jay gus wrote:
the main method runs automatically, but how do i call the method jay to make that run?

Recommended reading http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

are System and out super classes that are being referenced by Println ?

No. "out" is a static variable in the System class which has the println method (not Println. In Java case does matter). http://download.oracle.com/javase/6/docs/api/java/lang/System.html
http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
being very new to java programming, those websites you referenced, which i spent the last hour or so trying to utilize to solve my problem, were entirely useless to me.
thanks anyway.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

jay gus wrote:were entirely useless to me.

http://download.oracle.com ?

Welcome to JavaRanch!
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
class hiya {
public static void main(String args[]){
System.out.println("one");
}

public static void jay(String args2[]){
System.out.println("two");
}


//I want to run method jay here


}





I have been at this for hours. Will someone PLEASE tell me how to call method jay from outside the main method brackets?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

jay gus wrote:I have been at this for hours. Will someone PLEASE tell me how to call method jay from outside the main method brackets?

You can't.

Method calls have to be inside methods. You cannot put arbitrary executable statements, like method calls, outside of a method. Put the call to your method inside your main() method, like this:

Why does your method jay have a String args2[] argument?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jesper de Jong wrote:
Method calls have to be inside methods.

or use blocks. if the method is static then you can also call the method from static initialization block.
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
I have no idea why my method jay has a String args2[] argument. i'm very new, and since my main method had that I just put it on my jay method also.

Thank you for your post. I actually just figured it out before reading your post.
This was my solution:


why does this work? I thought that (String args[]) was identifying the method as containing a string and args was the name of that string variable.
why does my main method need (String args[]) but not my jay method?

keep in mind i'm new to programming
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
jay gus wrote:why does my main method need (String args[]) but not my jay method?


Think of the scenario where in you have to give some input to your program. Arguments can be passed to your program for execution. All your jay() method is doing is printing something for which you do not need any input arguments. Whether you pass arguments or not the method will work the same.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

The (String args[]) is the argument list: it says what values must be passed to the method when you call it. In this case, you're saying that an array of String objects must be passed to the method.

But you don't need that for your jay method, in fact, it doesn't need any arguments at all, so you can just as well leave them out.

Your example works because you are passing the String array that the main method received to your jay method. But your jay isn't using the arguments.

The main method needs to have this specific declaration: public static void main(String[] args) because Java is looking for a method that has exactly that declaration when you start your program. In other words, the entry point of your program is a main method with that exact declaration. The args array can contain extra arguments that you can specify on the command line when you start your program.

Oracle has a very good set of Java tutorials about many different subjects, from beginner to advanced topics. Have a look especially at these ones:

Getting Started
Learning the Java Language
Essential Classes
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
Thanks for all your help, guys. I'm understanding everything you're telling me and I think i'm making some progress.

public static void main(String args[]){

this is saying that in order for the method main to do it's job, the string variable named args needs to be passed to the method. Do I have that right?
In that case, where the heck is args? I didn't declare any string named args.

i called my method jay with : jay();
if method jay returned a value then how would i need to call it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

jay gus wrote:this is saying that in order for the method main to do it's job, the string variable named args needs to be passed to the method. Do I have that right?

Yes, that's right.

The name "args" stands for "arguments". By the way, it doesn't have to be named "args", you can give it any name you like, but "args" is a convention that almost everybody uses. Argument variables are declared right there, between the round braces.

When you run your Java program from the command line, you can pass arguments to it. Have a look at this program:

It prints out the content of the args array. Compile it, and run it like this:

java Example Hello Java World

It will print out:


Hello
Java
World


The three words Hello, Java and World are passed as three elements in the args array.

The void in your method declaration means that your method doesn't return any value. If you want it to return a value, then replace the void by a variable type, and add a return statement to the body of the method to return a value. For example:

Note that the add method returns an int instead of void, and it contains a return statement to return the result.

Have a look at Defining Methods in those Java tutorials from Oracle.
jay gus
Greenhorn

Joined: Sep 05, 2011
Posts: 7
Thank you so much guys. I've been doing tutorials and such in my spare time for a few weeks. I was so frustrated, posting on a forum was my last resort. Understing better how the indiviual methods work, everything I've been reading in the last weeks just clicked in place.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36495
    
  16
jay gus wrote: . . . System.out.Println is a method within the Java API . . .
No, it isn't. There is no Println method. There is println, however. You need to be very careful about such spellings. The compiler can give you all sorts of error messages because you forgot the difference between p and P.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

jay gus wrote:if method jay returned a value then how would i need to call it?

You don't have to do anything different to call a method with or without a return value. You can ignore the return value entirely, if you want, when you call it.

HOWEVER...if that value is important to you, you probably would want to store it in a variable. You need to have the correct type of variable to store it - i.e., if it returns a String, you can't try and save it in an Integer.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very beginner question: calling methods
 
Similar Threads
Another static inner class question
methods will execute?
System.exit()
About overriding
Please Help me out...