Terms
- Local variable
- Instance variable/members
- Class variable/members
- Fields (they are variables)
- Methods (Functions in C++)
- Method Overloading
- Static Factory Methods
- Constructor
- Varargs (variable length of argument list)
- Packages
- Class Access Control Modifiers (public, private, default, protected)
Access Level | Classes outside the package | Classes in the same package | Child Class | Same Class |
---|---|---|---|---|
public | yes | yes | yes | yes |
protected | no | yes | yes | yes |
default | no | yes | no | yes |
private | no | no | no | yes |
Example
import java.io.FileReader; class MyClassName { int localVar1; double localVar2; // public field name accessible by obj/reference public int instanceVar3; // class field name available without obj creation // it is accessible by className.classVar static int classVar1; // Constructors public void MyClassName (int ageValue, double phoneNumber) { } // Method Overloading // 1. number of parameters // 2. datatype // 3. sequence of parameters public void MyClassName (char gender) { } void methodName1 (listOfArguments) { this.classVar1 = 0; this.localVar1 = 2; this.localVar2 = 3; } int methodName2 (int inputNumber, int a, int b) { } ClassName2 getData () { return new ClassName2(); // return a handle/reference } public static void main (String[] args) { } } // To create an object (an object reference to be precise) // use: new MyClassName; // without new to create and construct the object, // myInstanceName == null; // public variable and method can be accessed by // objectReference.methodName // objectReference.fieldName MyClassName myInstanceName = new MyClassName(); myInstanceName.instanceVar3 = 24; if (book == null) { book = new Book(); // create the object/reference }
Class member versus Instance member
Java supports the notion of static members, which are class members that can be called without first instantiating the class.
public static int a; static public int b; static final int NUMBER_OF_MONTHS = 12; public static final float PI = (float) 22 / 7; public static int add (int a, int b) { return a + b; } // You can't call non-static members from a static method // Remember, if it is not declared to be static, it does not // exist until you create/construct the object/instance pub class StaticDemo { public int b = 9; public static void main (String[] args) { System.out.println (b); // will generate compile error! } } // To solve the above error, there are two solutions. // 1. Make b static // 2. Create an instance of the class and access b // by the object reference // You can only declare a static variable in a class level. // You cannot declare local static variables even if the // method is static.
Useful Packages
- java.awt.Component
- java.awt.Container
- java.awt.Object
- java.awt.Object
- java.applet.Applet
- java.io
- java.io.File
- java.nio.File
- java.io.FileReader
- java.io.PrintWriter
- java.io.FileNotFoundException
- java.io.PrintSteam
- java.lang.annotation
- java.lang.Boolean
- java.lang.Integer
- java.lang.Math
- java.lang.Object (toString)
- java.lang.reflect
- java.lang.Runtime
- java.lang.Scanner
- java.lang.String
- java.lang.StringBuffer
- java.lang.StringBuilder
- java.lang.System
- java.math.BigDecimal
- java.sql.Date
- java.time.Clock
- java.text.NumberFormat
- java.util.ArrayList
- java.util.Calendar
- java.util.Date
- java.util.LocalDate
- java.util.Locale
- java.util.Random
- java.util.Scanner
- java.util.*
EXAMPLE
// You cannot use new to instantiate an object when a class has a // private constructor, such as java.util.LocalDate. Instead, use // its static methods. LocalData today = LocalDate.now (); // such methods are called static factory methods. // Often time this is because these private constructors define // constants and members that should stay intact by the class users. // The static factory methods can invoke the private constructors // because they are in the same class. This way, the class creator // can restrict an object to contain only certain legitimate values // and make other values impossible.
import static java.lang.System.out; ... out.print("id"); out.println ("whatever you want to print");
No comments:
Post a Comment