0
class Nested
{
    int a,b,c;
    double s,ar;
public void getdata()
{
   a = 2;
   b = 3;
   c = 4;
}
public double semiperimeter() //semiperimeter( ) is the nested method of area( )
{
    double t = (a+b+c)/2;
    return(t);
}
public void area()
{
    s = semiperimeter();
    ar = Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
void display()
{
  System.out.println("Area of triangle"+ar);
}
 public static void main()
{
   Nested ob = new Nested();
   ob.getdata();
   ob.area();
   ob.display();
}
}

in above code why we called the function semiperimeter( ) through area( ) and with in area( )? .what is the benefit of nested member methods? A member function can be called by using its name inside another member function of the same class for that you do not need an object & directly without using the dot operator? why???? but to call the non static method we uses Objectname.functionname(); but in s = semiperimeter(); we are not using object and . operator?? why??

Sebastian Hoffmann
  • 2,815
  • 1
  • 12
  • 22
Manjeet Singh
  • 57
  • 1
  • 6
  • `semiperimeter()` can be written as `this.semiperimeter()`. You're allowed to write the name directly because it's in scope. They're both non-static methods and there's nothing special about them. I've never heard the term "nested member method" before (at least not in Java). This appears to be a C++ concept – user Jul 09 '20 at 15:31
  • please explain in briefly what do you mean by it is in scope?? – Manjeet Singh Jul 09 '20 at 15:37
  • `semiperimeter` is in the same scope as `area`, because it's in the same class. If `semiperimeter` was a static method and you had imported it, that would also mean that it was in scope/visible to `area`. However, in your main method, you can't directly use `area` because even though it's in the same class, `main` is static and doesn't know which instance of `Nested` to invoke `area` on. – user Jul 09 '20 at 15:39
  • but when i am trying to call area method directly without object with in main method, it is showing error “non-static method cannot be referenced from a static context”.what does this mean?? – Manjeet Singh Jul 09 '20 at 15:57
  • See https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context and https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static – user Jul 09 '20 at 15:58

0 Answers0