简体   繁体   中英

Calling a Method if another Method is called

I'm feeling curious. In Java, is it possible to nest a newly created method inside of a method? I ask this because in my book there are these practice exercises for each section (I'm currently on Objects/Classes) and there's a question that tells you to call a boolean method as true if another method - in the same class - was called (in the class that was called the boolean variable becomes true). Here's an example:

public void setTrue(int someRandomStuff) {
   this.someRandomStuff  = superRandomStuff; 
   // this is where the boolean variable comes into play
   trueOrFalse = true;
}

Now I was wondering is it possible to put another method inside of this method that calls if the method setTrue sets 'trueOrFalse' to true? Or would I have to create another method entirely? I'm only trying it this way because I haven't really read anything that said this wasn't possible. I've tried it like this and IntelliJ doesn't really show if it's wrong, so I'm gonna assume it must be possible.

Here's what I've tried for further clarification on what I mean:

public void setTrue(int someRandomStuff) {
   this.someRandomStuff = superRandomStuff;
   trueOrFalse = true;
   if (trueOrFalse) { 
     public boolean getTrueOrFalse() {
     return true;
   }
}

I'm honestly trying to learn why and how things work the way they do, rather than just do it because someone/something tells me that's the way it is and always will be.

If you're curious about the formatting or want exercises similar to this the book is called: Introduction to Java, Comprehensive Version (9th Edition).

不,您不能在另一个方法中有方法定义,必须在类中单独定义方法,否则该代码将无法编译

No you do not nest methods with each other, you list them all in a class. You can, however, call one method from another method both within the same class. You've done this many times, I assume. So it would look like:

public class Something {
    public void methodOne() {
        // does something
    }

    public int methodTwo() {
        // does somethingElse
    }

    public void methodThree() {
        int variable = methodTwo();
    }
}

You cannot nest your methods like that. Judging by your code it looks like you want to set a class variable. Set your method variable as true and class variable as true

boolean isMethodCalled = false;

public void doMethod(int someRandomStuff) {
   this.someRandomStuff = superRandomStuff;
   isMethodCalled = true;
}

You then can define another method that returns your trueOrFalse class variable

public boolean getIsMethodCalled() {
   return isMethodCalled;
}

This will make isMethodCalled true when doMethod is called. Otherwise isMethodCalled will return false.

Method nesting in java is not allowed. It will give the compilation error. If your requirement demands calling another method from one method you can declare your methods within the same class as private and call them.

public class MethodNesting {
    public void method() {
        myinnerMethod();
    }

    private void myinnerMethod() {
        // Do Something
    }
}

And If you are curious to know more have a look at this

Can methods in java be nested and what is the effect?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM