Source File: filename.java to compile: javac filename.java -> after compilation filename.class [byte code] to execute: java filename Package Statement[optional] //syntax: package array import statements;[optional] //only when we are importing class from diff //package //Class Declaration: [Access Modifiers(public/default)] [Non Access Modiferes(Abstract/final)] class ClassName extends super class/implements interface { //Variables Section -> Reference variables(classname)/Primitive //variables(int,char,long,double,float,short,boolean and byte) 1) //instance Variables -> [Access modifiers(public/private/protected/default)] [final] type variableName; 2) //class variables/static variables just like instance variables but with static word : one common copy for all objects [Access Modifers] static [final] type variableName; //Constructor -> Method with No return type, class name as method name [AccessModifers(public/default/private/protected)]ClassName(type parameterName) { //initialize instance variables } //Instance Methods [AccessModifers(public/private/default/protected)] [final/abstract/synchronized] returntype methodName(type parameterName) { //logic } //Class method/static method: object is not required [AccessModifers(public/private/default/protected)] static returntype methodName(type parameterName) { //logic } // method ends }//class ends Object Creation: ClassName referenceVariableName = new ClassName([parameters]); // single line comments /*multiple line comments */ A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types. Class: A template that describes the kinds of state and behavior that objects of its type support. ■ Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class. ■ State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state. ■ Behavior (methods) When a programmer creates a class, she creates meth- ods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.
No comments:
Post a Comment