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.

Re: Java Code Help - Calling Method

Simple, in fact you're already calling a few methods in your code. They just happen to be methods you didn't write

The general from:

method_name(method_parameters)

method_name is the name of the method, and method_parameters are the arguments you pass to your method (0 or more, separated by commas).

methods can also return ONE value (unless void, then method must return ZERO values). The value is returned using the return keyword (how convenient). However, even though a method can only return one value/run, it can have multiple return statements.

So how do you use the return value in the calling method? A simple way to think of it is this: take the entire method_name(method_parameters) and replace it with the return value

int a = doIt();
// say doIt() returns 5, the command can be thought of as looking like this:
int a = 5;

here, there are 2 return statements, but each run it is only possible that 1 return statement ever gets executed (can you see why?)

Also, another thing about return statements: They stop the execution of that method and return back to the calling method.

public static void doIt()
{
return;
// this next line will never get executed
System.out.println("You'll never see this!");
}

Because of this, some compilers (Eclipse and NetBeans, plus a few others) will throw a syntax error when you try to write statements that can can never be executed.

So, let's fix your code. As a general rule of thumb, don't have "side-effects" in sub-methods. Side effects are things that performs some output (screen, sound, etc.), or ask for some input. There are some times this is unavoidable, but if it's avoidable, avoid it

Re: Java Code Help - Calling Method

the static keyword means you want the function to work "statically" rather than on instanced variables. Here's a brief explaination of static and instance:

Java is completely object oriented. So, everything has to be run from an object. However, when the JVM starts, it creates a "static" version of each class. There is only one static version of each class/JVM. To call a static method, you use:

className.staticMethodName(params);

Instance variables are kind of like copies of the object. They have different values between objects, and must be created before they can be used. To create an instance variable, use the "new" keyword.

new SomeObject();

To call a method in an instanced object, you use the object variable handle:

SomeObject a = new SomeObject();
a.doIt();

*Note: Java will let you use static methods/fields with instance variables, but realize that it is still operating on the static object, not the instance object. For this reason, if you're accessing something that's static, make it a habit to use the above mentioned method for static access.