Results 1 to 3 of 3
  1. #1
    LifeWithJava is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default Using accessors properly

    Hi guys,

    I am learning how to use the get-method properly and a simple trial i came up with somehow does not allow the accessor to work, the compiler says 'missing method, body or declare abstract'...would appreciate help on this..i think it's a minor problem, i juz cant see it yet...

    public class test
    {
    private int priInt; //private variable only accessible from this class


    public test(int priInt) //constructor method
    {
    this.priInt=priInt;

    }
    public int get_priInt(); // this is an accessor
    {
    return this.priInt;
    }

    public int addNumber(int m, int n) //methods to add and subtract 2 numbers
    {
    System.out.println("hi");
    return (m+n);
    }
    public int subtractNum(int m, int n)
    {
    return (m-n);

    }
    }


    Looking forward to hearing from y'all! :)

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Smile

    Remove the semicolon from the line public int get_priInt(); and it should work.

    If you want some more tips, read on:
    • When posting code here, it helps people if you surround it with the code tags.
    • I normally indent code, since it makes it more readable.
    • It is convention to capitalise class names (see http://java.sun.com/docs/codeconv/ht...ions.doc8.html). Similarly, most Java programmers use the 'camel case' for methods and variable names (e.g. getPriInt).
    • There is no need to use the this keyword in a getter method, it is mainly used when 'a field is shadowed by a method or constructor parameter' (see Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)).
    • Good documentation is quite an art, I don't think I have got the hang of it yet! If you precede your methods with a comment that starts like /**, you will find later on in your learning that you can use something called Javadoc, which is a very useful.
    • If methods do not use any of the variables of a class, you can declare them as static. This means that they can be used without having to create an object (instance of your class Test). Convention is that static method names start with a capital.

    If you followed these suggestions, your code would look like:

    Java Code:
    /**
     * A class which helps me learn accessor methods
     * It stores an integer and can do addition and subtraction
     */
    public class Test
    {
        private int priInt; //private variable only accessible from this class
    
        /**
         * Creates a new Test object, which stores the integer which is supplied
         */
        public Test(int priInt)
        {
            this.priInt=priInt;
        }
        
        /**
         * Returns the integer this object stores
         */
        public int getPriInt()
        {
            return priInt;
        }
    
        /**
         * Adds any two numbers
         */
        public static int AddNumber(int m, int n) //methods to add and subtract 2 numbers
        {
            System.out.println("hi");
            return (m+n); 
        }
        
        /**
         * Subtracts any two numbers
         */
        public static int SubtractNum(int m, int n)
        {
            return (m-n);
        }
    }
    Good luck with learning!
    Last edited by Paul Richards; 12-23-2008 at 12:39 PM.

  3. #3
    LifeWithJava is offline Member
    Join Date
    Dec 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks Paul, it was the ';' after all, lol..will remember this from now :)

    I have a query regarding the convention for naming methods though, in school we've been naming methods this way: firstName, empHour, userId..thus when you mention the convention, albeit for static methods, should be (according to the e.g. above): FirstName, EmpHour, UserId), i am getting a little confused. A quick check on google (sorry I cant post the links cuz I need to surpass 20 posts-limit administered on this site) gave me the impression that we may need to be flexible, esp in a team environment...hmm..but i'll surely look out for this in others' programs from now.

    Thanks for the tips, i surely appreciate them and will adhere to them :)

Similar Threads

  1. Image not showed properly in struts
    By ganesan in forum Web Frameworks
    Replies: 4
    Last Post: 01-13-2009, 09:21 AM
  2. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 10:06 AM
  3. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 PM
  4. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM
  5. how to properly express adding...
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •