Method inside method

can someone please tell how can I use a method inside another method in same class?

for example

public void method1(){
   //do stuff
}

public void method2{
   //If I want to use method1() here how can I use it?
}
1 Like

You can’t really in Java (maybe they’ll bring it in soon? it sure is useful). If you want to you can do this:

//in SomeClass
  interface MyWorld {
    void world();
  }

  public void hello(){
    System.out.println("hello");

    MyWorld world = new MyWorld() {
      public void world() {
          System.out.println("world");
      }
    };
    world.world();
  }

then in main

  public static void main(String[] args){
    SomeClass mn = new SomeClass();
    mn.hello();
  }

Output is:

hello
world

import java.util.ArrayList;
public class PrimeDirective{


  public boolean prime(int num){
    if(num == 2) return true;
    if(num<2) return false;

    for(int j=2;j<num;j++){
      if(num%j == 0){
        return false;
      }
    }
    return true;
  }


  public ArrayList<Integer> firstNPrime(int x, int[] numbers){
    ArrayList<Integer> nPrime = new ArrayList<Integer>();
    for(int k = 0; k< numbers.length; k++){
      this.prime(numbers[k]); // << number
      if(prime(numbers[k])){ // if number is Prime
        while(nPrime.size() < x){
          nPrime.add(numbers[k]);
          break;
        }
      
      }
    }
      return nPrime;
  }

  public static void main(String[] args){
    PrimeDirective pd = new PrimeDirective();

    int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

    System.out.println(pd.firstNPrime(3,numbers));


  }
}

but here I have used a method inside another method.
how?

Oh my bad, I thought you said to define a method inside another method.

so is this the way to use a method inside another method?

i used the prime() method inside firstNPrime()

D’oh, I’m seeing things.

Yes, then that’s how you do it haha. @nabeelkhann I need my coffee.

but in another I am getting error. This is just irritating as well as hilarious.
Enjoy your coffee.

1 Like

I got confused because you’re writing fibonacci code (which is not that easy), I thought you were asking the harder question of defining a method inside another. @nabeelkhann which is impossible in Java but really easy in python or javascript.

What’s the error? Can you copy paste or screenshot?

  public ArrayList<Double> getRidePrices(){
    ArrayList<Double> ridePrice = new ArrayList<Double>();
    this.unlimited30Price(int noOfDays, int noOfRides);
    ridePrice.add(unlimited30Price(int noOfDays, int noOfRides));
    this.unlimited7Price(int noOfDays, int noOfRides);
    ridePrice.add(unlimited7Price(int noOfDays, int noOfRides));
    ridePrice.add(payPerRide);
    return ridePrice;
  }

error message

TransitCalculator.java:34: error: ‘.class’ expected
this.unlimited30Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:34: error: ‘;’ expected
this.unlimited30Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:34: error: ‘;’ expected
this.unlimited30Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:35: error: ‘.class’ expected
ridePrice.add(unlimited30Price(int noOfDays, int noOfRides));
^
TransitCalculator.java:35: error: ‘;’ expected
ridePrice.add(unlimited30Price(int noOfDays, int noOfRides));
^
TransitCalculator.java:35: error: ‘;’ expected
ridePrice.add(unlimited30Price(int noOfDays, int noOfRides));
^
TransitCalculator.java:36: error: ‘.class’ expected
this.unlimited7Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:36: error: ‘;’ expected
this.unlimited7Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:36: error: ‘;’ expected
this.unlimited7Price(int noOfDays, int noOfRides);
^
TransitCalculator.java:37: error: ‘.class’ expected
ridePrice.add(unlimited7Price(int noOfDays, int noOfRides));
^
TransitCalculator.java:37: error: ‘;’ expected
ridePrice.add(unlimited7Price(int noOfDays, int noOfRides));
^
TransitCalculator.java:37: error: ‘;’ expected
ridePrice.add(unlimited7Price(int noOfDays, int noOfRides));
^
12 errors

what happened buddy?

this.unlimited30Price(noOfDays, noOfRides);
// yours is: this.unlimited30Price(int noOfDays, int noOfRides);

I’d probably run the method like this.
I’m still not sure where you are getting noOfDays and noOfRides but that should tackle one part of the error.

1 Like

I have only shared the code from where I was getting error noOfDays these all have been defined earlier.

I got the answer Actually instead of int noOfDays, int noOfRides it needs to be simple noOfDays, noOfRides.

no need to declare data type again

1 Like

Perfect. Got to the bottom of it.

Plus now you have the bonus trick definition of method inside another (can be useful though I haven’t written in java in ages). Don’t know if it was worth your frustration though …

1 Like

yeah , now I guess your coffee must have been cold now. So enjoy

1 Like