Instance variables are those variables that are specific to each class while class variables are those variables that are common among a class. In other words, instance methods or variables are those methods or variables that are specific to each object or instance of the class. On the other hand, class variables or methods are those methods or variables that represent class-wide situation (Horstmann & Cornell, 2002). For example the class Person, instance variables may include age and name while class variables may include species.
The super keyword
The super keyword is used to signify a call to the instance of the class that is being extended. It can only be utilised in the constructor of the class that is being extended, and it is imperative that it should be the initial call within the given constructor. Additionally, the super keyword can be employed anywhere to reference the method of the class under extension.
Example
public class A{ private String x = “A”;
private String y = “B”;
public class A(){ System.out.print(toString());}
public String toString(){return “x:” +a+”b:”+b;”}}
public class B extends B{ private String x=”5” ; private String y=”6”;
public B (){ super();
} (Fleury, 2000).
The super keyword used in this example will initiate the default constructor of A which will further call the toString instance of A.
Exception propagation
Exception propagation refers to the concept that elaborates the travel of exception from method to method, through the call stack until it is trapped. For instance, if method x() calls method y(), which calls method w() , which in turn calls method z(). In the event that method z() throws an exception, the exception will travel from z to w to y to x, unless one of the methods involved catches the exception.
void x(){ int outcome=200; // exception
}
Void y(){ w();
}
Void x(){ try y();} catch (exception e){
System.out.print(“Exception handled at this point”)
}
}( Torgersen et al., 2004).
GUI and Event handlers
import java.swing .JTextField;
{ private JButton greenButt;
private JButton redButt;
private JButton blueButt;
public Button ()
{
greenButt= new JButton (“Green”);
redButt = new JButton (“Red”);
blueButt = new JButton (“Blue”);
add(greenButt);
add(redButt);
add(blueButt);
}
Radio buttons and checkboxes
Radio button and checkboxes are similar given that they offer the user an opportunity to select or make a choice from a variety of choices within an application. On the other hand, the two differ in their application and use given that they are used in different situations. In most cases, radio buttons are used when the question asked requires only one answer. Conversely, check boxes are used when you want to offer the use an opportunity to select more than one choice (Arnold et al., 2000). Additionally, check boxes have the ability to be swapped independently of each other while radio buttons work collectively where only one is active at a time.
Writing recursive methods
A recursive method that given n computes the nth term for the sequence
{ if (n< =1) return n;
Return fib(n-1) + fib(n-2);}
Pub static void main (String args()){
System.out.print (fib(n));
} } (Dietel, 2009).
Generic classes
Public void set (T m) }{this.m = m}
Public T get () {return m;} (Wellings, 2004).
Lists
Arrays and linked lists can be applied in different situations within a program. There are several situations when linked lists are preferable than arrays (Liang, 2009). For instance, when random access to any element is not required, when items can be added in the middle of the list when the number that will be on the list is not known, and when constant-time deletions or insertions from the list are required. On the other hand, arrays are preferable when memory is a concern and when random or indexed access to elements is required.
References
Arnold, K., Gosling, J., Holmes, D., & Holmes, D. (2000). The Java programming
language (Vol. 2). Reading: Addison-wesley.
Dietel, P. (2009). Java how to program. PHI.
Fleury, A. E. (2000, May). Programming in Java: student-constructed rules. In ACM SIGCSE
Bulletin (Vol. 32, No. 1, pp. 197-201). ACM.
Horstmann, C. S., & Cornell, G. (2002). Core Java 2: Volume I, Fundamentals. Pearson
Education.
Liang, Y. D. (2009). Introduction to Java programming: brief version. Pearson Prentice Hall.
Torgersen, M., Hansen, C. P., Ernst, E., von der Ahé, P., Bracha, G., & Gafter, N. (2004,
March). Adding wildcards to the Java programming language. In Proceedings of the 2004
ACM symposium on Applied computing (pp. 1289-1296). ACM.
Wellings, A. (2004). Concurrent and real-time programming in Java. John Wiley & Sons.