1

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).

halfer
  • 19,824
  • 17
  • 99
  • 186
N.P
  • 187
  • 15
  • Your code was without any indentations making it all left justified and making it difficult to read, understand and debug. I have re-formatted your posted code by giving it consistent indentations, and by making sure that all code on the same block is on the same indentation level. In the future, you'll want to do this yourself. Your cooperation in this will be greatly appreciated and will likely improve your chances of getting a decent and prompt answer. – Hovercraft Full Of Eels Sep 21 '14 at 01:33
  • Why not pass your method with a param? – shinjw Sep 21 '14 at 01:36
  • @HovercraftFullOfEels Sorry, the interface is a bit difficult to use on a tablet; highlighted text is prompting my iPads features. shinjw What do you mean by that? Not fully understanding what you're trying to get at, please clarify. – N.P Sep 21 '14 at 01:40

4 Answers4

0

No, you cannot have a method definition inside another method, methods must be defined separately in your class, that code will not compile

0

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();
    }
}
NoseKnowsAll
  • 4,593
  • 2
  • 23
  • 44
0

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.

shinjw
  • 3,329
  • 3
  • 21
  • 42
  • Hmm I'm not sure if that is what is being asked in the problem in the book. I would just look at the answers, Liang (the author of the book) doesn't have answers to this particular section on his online text version of the book. The question asks you to create a method that returns true if another method was called. The perk to that is, in the method - that was called - it sets your boolean variable to true. So these are supposed to be two independent methods. That's what I'm not getting and why I asked this question. – N.P Sep 21 '14 at 01:47
  • You will need to set up class variables to make this happen – shinjw Sep 21 '14 at 01:48
  • Oh, Just saw your edit after refreshing the page and that is very close to what I going to ask next - in my edit. The variable 'methodTrueOrFalse' did you not have to declare its type, or is that just omitted? – N.P Sep 21 '14 at 01:50
  • Is this looking closer to what you are trying to do? – shinjw Sep 21 '14 at 01:53
  • 1
    Yes. It's so close that I might mistake you for standing behind me now. Haha. Thanks bunches for clarifying this and providing detailed explanations. – N.P Sep 21 '14 at 01:55
  • http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html This might be helpful to explain what is going on. – shinjw Sep 21 '14 at 02:01
0

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?

Community
  • 1
  • 1
Pradeep Kr Kaushal
  • 1,506
  • 1
  • 16
  • 29