Introduction to Basic Java Concepts
Methods- creation/calling
A method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method).
import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { popUp(); } public static void popUp() { JOptionPane.showMessageDialog(null, "Pop"); } }
A method can be passed any Variable you would like.
import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String word = "Example Text"; popup(word); } public static void popup(String passedWord) { JOptionPane.showMessageDialog(null, passedWord); } }
void/return (Procedure/Function)
Void means that the method will not return any variable. If the keyword return is used in a void method you will get an error. See popUp above for an example. If using Return then an outbound variable type must be defined in front of the method name. Using Return means that the method will return a variable via the outbound variable type. import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String word = getWords(); popup(word); } public static void popup(String passedWord) { JOptionPane.showMessageDialog(null, passedWord); } public static String getWords() { String userInput; userInput = JOptionPane.showInputDialog("Pass me a String"); return userInput; } } Static- what is it?
When static is applied to a method this means that once the class is compiled, the methods can be invoked. i.e. an instance of the class is not required to make the method usable. As mentioned above, a method may be declared as static, meaning that it acts at the class level rather than at the instance (object) level. Therefore, a static method cannot refer to a specific instance of a class (object). new- what's it do?
New creates an instance of an object. NOTE that object can be an int, string, class object etc) import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { Words testObject = new Words(); String textFromObject = testObject.getWords(); JOptionPane.showMessageDialog(null, textFromObject); } } ---In a separate .java file/Class--- public class Words { public String text; Words() { text = "Example Text"; } public String getWords() { return text; } } A new instance of a Words object is being created. Then getWords is being called on this object to return the string text. static methods in the class(s) you just created objects from
Note although the object testObject has been created below, the code shows how the static methods totalWordObject & getTotalWordObject do not need to be instantiated to be invoked. import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { Words testObject = new Words("one"); Words.totalWordObject(); JOptionPane.showMessageDialog(null, Words.gettotalWordObjects()); } } ---In a separate .java file--- public class Words { public String text; public static int total = 0; Words(String fail) { text = fail; } public static void totalWordObject() { total++; } public static int gettotalWordObjects() { return total; } } calling a non static method on an object
To call a non static method within an object, state the name of the object and then place a “.” followed by the name of the relevant method. import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { Words testObject = new Words(); String textFromObject = testObject.getWords(); JOptionPane.showMessageDialog(null, textFromObject); } } ---In a separate .java file--- public class Words { public String text; Words() { text = "Example Text"; } public String getWords() { return text; } } A new instance of a Words object is being created. Then getWords is being called on this object. overloading methods
If there are two methods of the same name, the parameters that are passed will decide which method gets invoked (i.e. the incoming signature decides which method to invoke) import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { popUp("String"); popUp(12); } public static void popUp(String passedString) { JOptionPane.showMessageDialog(null, passedString); } public static void popUp(int passedInt) { int totalInt = passedInt + 2; JOptionPane.showMessageDialog(null, totalInt); } } Deep Copy vs Shallow Copy
Deep Copy is copying the entire object (i.e. a brand new version of the object complete with data) as opposed to shallow copy which only copies a reference (i.e. memory address/pointer) to the original object which is being copied.
Diagram : Shallow Copy | Diagrams : Deep Copy |
Overwriting (via inheritance classes)
Where the method within a subclass overrides the identical method of a superclass. import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { Animal randomAnimal = new Animal(); Cat holly = new Cat(); holly.makeASound(); randomAnimal.makeASound(); } } ---In a separate .java file--- import javax.swing.JOptionPane; public class Animal { public void makeASound() { JOptionPane.showMessageDialog(null, "Generic Animal Sound"); } } ---In a separate .java file--- import javax.swing.JOptionPane; public class Cat extends Animal { public void makeASound() { JOptionPane.showMessageDialog(null, "Meow"); } }