Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Would the following be considered bad practice in Java?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Would the following be considered bad practice in Java?

    public class Call {
     
    	private int _CallNumber;
     
    	public void CallNumber(int value)
    	{
    	     _CallNumber = value;
    	}
    	public int CallNumber()
    	{
    	     return _CallNumber;
    	}
    }

    as opposed to :

    public class Call {
     
    	private int _CallNumber;
     
    	public int getCallNumber()
    	{
    	     return _CallNumber;
    	}
                  public void setCallNumber(int value)
    	{
    	     _CallNumber = value;
    	}
    }

    I simply prefer my accessors sorted by the field name, as opposed to get, get, get and set, set, set and I am curious as to whether or not the code at the top would be considered bad form in Java programming?
    Last edited by Newbie_71; September 19th, 2011 at 09:11 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,625
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Would the following be considered bad practice in Java?

    First bad practice is to post code outside of code tags which makes it very hard to read.
    Edit the post, press the Go Advanced button, select the code and press the #icon.

    The naming conventions for java have variable names start with lowercase letters.
    Method names are mostly verbs, not nouns. CallNumber is a noun, getCallNumber is a verb
    Class names are nouns.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Would the following be considered bad practice in Java?

    There are a few differences between the first and second, and a few things (and lack thereof) in both demonstrate bad practice (see Norm's post, and there are a few other things I'd considered bad practice). But focusing on your question at hand - standard practice is to follow the get/set methods of variable access. This is the standard of 'javabeans', some libraries depend upon method names designated as get/set, and more importantly its a standard that allows your code to be readable by others. Many IDE's allow you to automatically define the getter/setter methods for variables.

Similar Threads

  1. Looking for practice problems/assignments
    By tkgraham in forum Member Introductions
    Replies: 2
    Last Post: April 4th, 2011, 10:28 PM
  2. Practice Questions / Problems / Projects
    By Cuju in forum Java Theory & Questions
    Replies: 3
    Last Post: May 2nd, 2010, 09:00 PM
  3. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM