All posts by JazzerCoder

Static methods

Static

 

Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. A static method is distinguished in some programming languages with the static keyword placed somewhere in the method’s signature.

In statically typed languages such as Java, static methods are called “static” because they are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. Therefore, static methods cannot be overridden.

C++

Class MyClass {
public:
static float areaCirc (radio){
  //do some math here
  return area;
 }
};

Java

public class Mechanics {

    // add an instance variable for the object ID    
    private int cadence;
    private int gear;
    private int speed;

    //static method
    public static double speedURM (int dist, float time){
     // do some math here
    return urmSpeed;
}
}

Local and Anonymous Inner Classes in Java

Local and Anonymous Inner Classes in Java

Local Inner Classes in Java

If an inner class has been defined within a code block (typically within the body of a method), then such an inner class is called a local inner class. A local inner class is not a member of the enclosing class and hence it can not have any access specifier. A local inner class will have access to all the members of the enclosing class and it’ll have access to the local final variables in the scope it’s defined.

Example: a typical local inner class

public class EnclosingClass{
	...
		public methodName()
		{
			//...definition of the local inner class
			class LocalInnerClass 
			{
				...

			}
		...
}

Anonymous Inner Classes in Java

If an inner class has been declared without a name and within a code block (typically within the body of a method) then such an inner class is called an anonymous inner class. Since such an inner class doesn’t have a name associated, so it’ll be accessible only at the point where it is defined.

Example: a very common usage of an anonymous inner class

public class GUI extends JFrame{
	…
		public void buildComponents(){
		…
		button1 = newJButton();
		…
		button1.addActionListener(new java.awt.event.ActionListener(){
		public void actionPerformed(java.awt.event.ActionEvent ae){
		…
		…
		}
		});
		…
		…
	}
}

Since, an anonymous inner class doesn’t have a name, so it can’t have a named constructor. But, it can have an instance initializer. Access rules for anonymous inner classes are same as that of local inner classes. Similar to a local inner class, an anonymous inner class can also not have any access specifier attached to it.

Are anonymous inner classes same as local inner classes?

Yeah…, an anonymous inner class is a local inner class in all means except that it doesn’t have a name and hence it can’t be used at any other point except at the point where it has been defined. The other difference is that it can’t have a named constructor again for the same reason that it doesn’t have a name.