Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, January 10, 2014

Java naming conventions

In this post, we look into the basic naming conventions that everybody should know. The basic thing is how to write class names, variables, interfaces and methods name as per oracle standard naming conventions.




In professional environments, the benefits of coding standards are readability, maintainability and compatibility. Any member of a development should be able to read the code of another member. The coder who maintains a piece of code tomorrow may not be the coder who programmed it today. In addition, today’s enterprise solutions are so complex that multiple development teams unite to build a singular enterprise software application. With coding standards, distinct teams can rely o­n the way that they can interface with the code built by a separate team.

Classes and Interfaces
The first letter should be capitalized and if several words are linked together to form the name, the first letter of the inner words should be uppercase.
This sometimes called "camelCase".

  • Try to keep your class names simple and descriptive.
  • Use whole words-avoid acronyms and abbreviations 
Example of class:
Dog
Account
Factorial
PrintWriter

Example of interface:
Runnable
Serializable
FlyingInterface



Methods
The first letter should be lowercase, and then normal camelCase rules should be used.

Example:
run()
runFast()
getBalance()
doCalculation()
setCustomerName()


Variables

  • Like methods, the camelCase format should be used, starting with a lowercase letter.
  • Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.
  • Variable names should be short yet meaningful.
  • The choice of a variable name should be designed to indicate to the casual observer the intent of its use.
  • One-character variable names should be avoided except for temporary "throwaway" variables.

Example:
int i;
float buttonWidth;
long accountBalance;


Constants
Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators.

Example:
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int MIN_HEIGHT = 40;
static final int MAX_COUNT = 1000;



If you know anyone who has started learning java, why not help them out! Just share this post with them. 
Thanks for studying today!...

Wednesday, May 15, 2013

why static is not allowed in inner class

why static is not allowed in inner class or why static final is allowed in inner class

The idea behind inner classes ( non static inner class) is to operate in the context of the enclosing instance. Somehow, allowing static variables and methods contradicts this motivation.
http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.1.3

Inner class are like an instance attribute of enclosing object. Also static means to work without instance at first place.

So it's doesn't make sence to allow staic feature in inner classes.

Let take this example:

 class pardeep{
   public String name;
  }

If you create two instance of this pardeep

pardeep a= new pardeep();
a.name="kumar";

pardeep b=new pardeep();

b.name="kumarOne";

It is clear the each has own value for the property name.


The same happens with inner class, each inner class instance is independent of the inner class instance.


So if you try to create counter(static) attribute , there is now way to share the value across two different instances.


class Pardeep {

  public string name;
  class kumar{
    static counter;
  }
}

when you create two instance a and b , what would be the correct value for the static variables counter? It is not possible to determine, because existence of

Kumar class depends completely on each of the enclosing object.
That why static is not allowed in inner.

Note: This is also the reason that when class is declared static, it doesn't need any living instance to live itself.


If this counter is constant, then there will be no problem because value is not going to be changed.

That why final make them(counter) constant once initilazed, is a compile time contant value.
i.e, final static counter;
That why final static is allowed in inner class


If you know anyone who has started learning Java, why not help them out! Just share this post with them.Thanks for studying today!...