jb0314
jb0314

Reputation: 13

When check if method true, the method is called again?

Ok so in my program I have two classes, LinkPhone.java, and Frame.java. In the LinkPhone part it calls a function to determine if it is true, and then if it is do something. But then I call the function and use an If statement to check it, it recalls the statement from the If statement. Like in the console it says "DEBUG: Frame init success" twice. Why does it call the function twice and how would I fix it?

LinkPhone.java:

Frame.initFrame();
if(Frame.initFrame() == true){
    return;
} else {
    return;
}

Frame.java:

public static boolean initFrame(){
    try {
        JFrame frame = new JFrame("Link Phone");
        System.out.println("DEBUG: Frame init success");
        return true;
    } catch (Exception e) {
        System.out.println("DEBUG: Frame init failed!!!");
        return false;
    }
}

Upvotes: 0

Views: 1154

Answers (4)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're calling your method twice!

Frame.initFrame();
if(Frame.initFrame() == true){
    return;
} else {
    return;
}

Just call it once:

// Frame.initFrame();  // no need for this one

if(Frame.initFrame()){
    // hopefully you do more in here!
    // return;
} else {
    // hopefully you do more in here!
    // return;
}
return;

Some side notes:

  • I would avoid calling a class "Frame" since that would clash with a class that is part of the Java core classes. Give it a more descriptive name to avoid future problems.
  • It looks like you're calling static methods. This is OK if indicated, but over-use of static methods and variables risks increasing the connectedness of your program, i.e., it can increase code coupling, something that in larger programs can lead to increased complexity and risk of bugs. Java is an OOP language for a reason, since proper use of OOP techniques helps hide data, reducing bugs and increasing code re-use.
  • Both of your if and else code blocks have a return call. Better to simplify the code and get the return calls out of the blocks. Simply call return after the both blocks.
  • Again hopefully your if and else blocks hold more code than just matching return statements.

Upvotes: 4

Mshnik
Mshnik

Reputation: 7032

It's called twice because you write Frame.initFrame() twice.

Frame.initFrame();                //Once
if(Frame.initFrame() /* Twice */ == true){
    return;
} else {
    return;
}

If you only meant to call it once and want to store the result, try this:

boolean ok = Frame.initFrame();
if(ok){ //Use result of call
    //Do stuff in the event the initting went correctly
    return;
} else {
    //Do stuff in the event the initting failed
    return;
}

Or, event more compactly:

if(Frame.initFrame()){ //Use result of call
    //Do stuff in the event the initting went correctly
    return;
} else {
    //Do stuff in the event the initting failed
    return;
}

Upvotes: 0

Yes. Every time your program executes Frame.initFrame() it calls the method. (That's what the () syntax means)

If you want to call it once you can do this (without the extra call before it):

if(Frame.initFrame() == true) {

or this, if you prefer having the method call on a separate line:

boolean result = Frame.initFrame();
if(result == true) {

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140299

You need to keep hold of the result from the first time you call the method:

boolean result = Frame.initFrame();
if(result) {
    return;
} else {
    return;
}

Upvotes: 0

Related Questions