Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very beginner question: calling methods

 
jay gus
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jay gus
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay gus wrote:were entirely useless to me.

http://download.oracle.com ?

Welcome to JavaRanch!
 
jay gus
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic