Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following statement

Java
public double getValue(double new_value)
{

getValue = 5+8;

return getValue
}

<pre>



in the driver class the following code is written


s.getValue(0);







Would this work or not?

What I have tried:

I am just confused about the rules for this one
Posted
Updated 16-Mar-17 22:54pm
Comments
[no name] 16-Mar-17 18:32pm    
"Would this work or not?", why would you not try it and find out for yourself?
Member 13064243 16-Mar-17 18:40pm    
I did but nothing happens so I don't know
[no name] 16-Mar-17 19:29pm    
How, exactly, could "nothing" happen?

Well, it would compile and run. Try, for instance:
Java
class Foo
{
  double getValue;

  public double getValue(double new_value)
  {

    getValue = 5+8;

    return getValue;
  }

  public static void  main(String args[])
  {
    Foo foo = new Foo();
    double g = foo.getValue(5);
    System.out.format("getValue is %g\n", g);
  }

}

However, such a code would be a nightmare for the poor maintainer: getValue very name suggest a 'getter' method, that is a method returning a value without changing the state of the object. As matter of fact its body changes the state of the object.
Moreover, the passed newValue parameter has no effect in the method body.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900