Define method within the body of an outo method in Java

0

Hello, I was searching for Java and I came across the following code:

cliente.getInetAddress().getHostAddress()

I did not quite understand. Is this a method within another method? I tried to do:

public class Post {
    public void Post(){
        public void test(){}
    }
}

But NetBeans accuses the code is wrong. Well, I'd like to know how this works (if you could implement a simple example code)

Link to the code I found

    
asked by anonymous 08.06.2015 / 03:42

1 answer

6

On line

cliente.getInetAddress().getHostAddress()

First the client variable has this method getInetAdress() , it another object that in turn has another method getHostAdress()

The technical term is chaining - chained call

An example would be:

public class Pessoa{

    private Date dataNascimento = new Date();

    public Date getDataNascimento(){
        return dataNascimento;
    }
}



public class Main{
   public static void main(String... args){
   Pessoa p =new Pessoa();
   System.out.println(p.getDataNascimento().toString())
   }

}

So, in the above code I call a toString() method from the% object Date returned from the Person class. The toString() method is defined in the Date class, not the person class:

link

    
08.06.2015 / 03:53