Friday, August 13, 2021

# 3. Class in Java

Syntax and Rules for class


If we want to write something in the class room, we write it in the note book. 

Right! 

Book is a container which holds some documentation.

In the same way 

In java if we want to write a program that program should be inside a container called class.

Java is a programming language that is nothing but set of rules, syntaxes and regulations.

 A class is a container or a placeholder where we write program.

class is a fixed/standard keyword in order to convey it as a class, all should be in small letters.

In world everything has a name to call it in future/ to refer in future. 

In the same way in java also every class must have some name to call.


Rules to follow class name:

1. Class name must and should start with alphabets.

2. Class name should not start with numbers or special characters otherwise we will get compile time error.

3. Class name can contain numbers in between

4. Every class name must and should start with capital letter.

5. If a class name contains multiple words each word should start with capital letter.

6. Class name should be more descriptive to make it easier to understand

7. Class name should not contain special characters except '_'  underscore and '$' dollar symbols.

Ex: Flipkart_Electronics_Mobile


How to define a class:

-> <access specifier>    class    <class name>

{  // start of the class

-----------------------

-----------   //statements

----------------------

}  // end of the class

For example consider Facebook, when we post something in Facebook we have some options/restrictions like public, private, only me, only mutual friends, family. We chose based on our choice.



Access specifiers: Restricting the access

Specifying access of a class 

If we want to keep some restrictions on class we will go for access specifiers.

Types:

1. Public 

2. Private

3. Protected

4. <default>

It is not mandatory to set access specifier, its an optional. Even though if we don't specify any access specifier, by default it will become default.

Access specifier is optional.

Class keyword is mandatory.

Class name is mandatory

{ } are mandatory

>>> public class Myprogram {

statements

}

>>> public class My_Program {

statements

}








No comments:

Post a Comment

# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...