Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Why it does not work?</h2> <p>You have to add return types to <code>getAge()</code> and <code>setAge()</code>.</p> <p><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html" rel="nofollow noreferrer">Java Language Specification</a> notes that method declaration should follow this format:</p> <p>MethodModifiers(optional) TypeParameters(optional) Result MethodDeclarator Throws(optional)</p> <p>Relax, lets look at this slowly:</p> <ul> <li>MethodModifiers(optional) - You can mark each method as private/public/protected. If you omit this parameter method is package. There are few more modifiers, but these are most interesting for You now</li> <li>TypeParameters(optional) - It is about generic methods. You can safely skip this one now</li> <li>Result - This one is important. Every method must explicitly say what it returns. This was missing in Your example.</li> <li>MethodDeclarator - name of Your method.</li> <li>Throws(optional) - if Your method will throw exceptions You have to write it here. For now You can safely skip it.</li> </ul> <p><img src="https://i.stack.imgur.com/wIBfd.gif" alt="enter image description here"></p> <h2>Just show me the working code!</h2> <pre><code>public class Puppy{ int puppyAge; public Puppy(String name){ // This constructor has one parameter, name. System.out.println("Passed Name is :" + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void main(String []args){ /* Object creation */ Puppy myPuppy = new Puppy( "tommy" ); /* Call class method to set puppy's age */ myPuppy.setAge( 2 ); /* Call another class method to get puppy's age */ myPuppy.getAge( ); /* You can access instance variable as follows as well */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload