Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

/**

* JAVA BASIC SYNTAX


1. CLASS: The class is a blueprint (structure) of an instance(object) the class.
It can be defined as logical templates that share common attributes and methods.

2. OBJECT: The object is an instance of a class. An object has attributes and


methods.

3. METHOD: Function that can be performed by an object.

4. INSTANCE VARIABLES: Every object is assigned their own unique set of instance
variables.
The attributes of an object are created by the values that are assigned to these
instance variables.

5. COMMENTS: Comments are descriptive texts that do not affect the execution
process of the program at runtime.
Note that there are three types of comments, single line,multiline, and
documentation. (Samples sill be shown below)

6. SOURCE FILE NAME: The source file name should exactly match the class name with
the extension of Java.
Ex. BasicSyntax.java - Valid
basicSyntax.java - Invalid

7. CASE SENSITIVITY: Java is case-sensitive, which means identifiers xx,Xx, and XX


are different in Java.

8. CLASS NAMES: The naming convention for classes in java is the Pascal Case where
each first letter
of a word must be capitalized.
Ex. ThisIsSomeClass, AnotherClass,UpperClass,Class

9. public static void main(String[] args){}: This method is what executes a


program.

10. METHOD NAMES: The naming convention for methods is the Camel Case where the
very first is in lowercase, but the
succeeding words must have their first letters in upper case.
Ex. thisIsSomeMethod, anotherMethod, lowerMethod, method

10. IDENTIFIERS: These are names used to identify various elements in a program,
such as variables, methods, classes, interfaces, and labels.
They are essentially the names you give to these program elements to refer to them
within your code.
Ex:int myVariable;
String firstName;
BankAccount account;
calculateInterest();
MAX_VALUE;

11. WHITE SPACES: A blank line (contains only white spaces). This doesn't affect
program execution.

12. ACCESS MODIFIERS: Access modifiers determine the visibility or accessibility of


classes, variables, methods, and constructors from different parts of the program.
In Java, there are four access modifiers:
public: The public access modifier allows the associated element to be accessible
from anywhere in the program, including other classes and packages.
private: The private access modifier restricts the visibility of the associated
element to only within the same class. It cannot be accessed from other classes or
packages.
protected: The protected access modifier allows the associated element to be
accessed within the same class, subclasses (in any package), and other classes
within the same package.
Default (no modifier): If no access modifier is specified, it is considered the
default access modifier.
The default access allows the associated element to be accessed within the same
package but not from classes in different packages.

12. KEYWORDS: Keywords, also known as reserved words, are predefined words in the
Java language that have special meanings and functionalities.
These words are reserved and cannot be used as identifiers (e.g., variable names
or class names).
Ex: return,new,public,private,static,int,final,void,if,else,throw,for
*/
public class BasicSyntax {
/**(Documentaion / Doc Comment)
* Sample Class -> This serves as a template for what a car must be made off
and what it can do
*/
static class Car{
// Instance variables representing the state of a car
private String color; // Color of the car
private int speed; // Speed of the car

// No-argument constructor to create a car with default values


public Car() {
color = "DEFAULT";
speed = 0;
}

// Constructor with parameters to create a car with specified color and


speed
public Car(String color, int speed) {
this.color = color;
this.speed = speed;
}

// Getter method to retrieve the color of the car


public String getColor() {
return color;
}

// Setter method to modify the color of the car


public void setColor(String color) {
this.color = color;
}

// Getter method to retrieve the speed of the car


public int getSpeed() {
return speed;
}

// Setter method to modify the speed of the car


public void setSpeed(int speed) {
this.speed = speed;
}

// Overridden toString() method to represent the Car object as a string


@Override
public String toString() {
return super.toString(); // Return the default string representation of
the Car object
}
}

//Allows a program to be executable (Single Line Comment)


public static void main(String[] args) {
/* (Multiline Comment)
* Creating an instance of the car class
* "Red" -> This will be assigned to the "color" attribute in the Car class.
* "99" -> This will also be assigned to the "speed" attribute in the Car
class.
*
* */
Car car = new Car("Red",99);
}
}

You might also like