Syntax and Rules for class
If we want to write
something in the classroom, we write it in the notebook.
Right!
A book is a container that
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 a set of rules, syntaxes, and regulations.
A class is a
container or a placeholder where we write a program.
class is a fixed/standard
keyword to convey it as a class, all should be in small letters.
In the world, everything
has a name to call it future/ to refer to in the 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 the alphabet.
Ex: Program, Electronics, Dolls, etc.--------> valid
2. Class names should not
start with numbers or special characters otherwise we will get compile time
error.
Ex: 78Program, 80Dolls, #animals -------> not valid
3. Class name can contain
numbers in between
Ex: Program20, Electronics4, Dolls50babies--------> valid
4. Every class name must
and should start with a capital letter.
Ex: Program, Electronics, Dolls, etc.--------> valid
5. If a class name
contains multiple words each word should start with a capital letter.
Ex: MyProgram, BabyDolls, ElectronicDevices---------> valid
6. Class name should be
more descriptive to make it easier to understand
Ex: BabyDollsStore, ElectronicDevicesShop, MyProgramForPractice---> valid
7. Class name should not
contain special characters except '_' underscore and '$' dollar symbols.
Ex:
Flipkart_Electronics_Mobile,
A java file can have any number of classes but it can have only one public class and the file name should be the same as the public class name.
How to define a class:
<access specifier>
class_keyword <class name>
{
-----------------------
-----------
----------------------
}
For example, consider Facebook, when we post something on Facebook we have some options/restrictions like public, private, only me, only mutual friends, or family. We chose based on our choice.
Access specifiers:
Restricting the access
Specifying access to 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 an access specifier, it's optional. Even though if we don't specify any
access specifier, by default it will become the default.
- Access specifier is optional.
- The class keyword is mandatory.
- Class name is mandatory
- { } are mandatory
Syntax:
public class
Myprogram {
statements
}
Syntax:
public class
My_Program {
statements
}
Comments in java
There are two
types of comments in java
- Single line comments
- Multi line comments/ block comment/ traditional comment
Single line
comments: it will starts with // in the beginning of the comment
// I am a single
line comment
Multi line
comment: it starts with /* forward slash and asterisk in the beginning of the
comment and ends with */ asterisk and forward slash.
/* I am a
·
Multi
line comment
I can be used in
java */
Documentation
comments:
These begin with a
forward slash followed by two asterisks and end with an asterisk followed by a
forward slash
/** this is a
documentation comment */
/** this is also a
Documentation
comment*/
When a
documentation comment begins with more than two asterisks Javadoc assumes that
you want to create a ‘box’ around the comment in the source code. It simply
ignored the extra asterisks
/************************
This is the start
of a method
*************************/
This will retain
just the text “ This is the start of a method” for the documentation.
Now define the class using comments
-----------------------------------------------------------------------------------------
Example 1:
// class definition with multi line comments
// access specifier, class keyword, class name
public class My_Program { // start of the class
/* statements
statements
statements
*/
} // end of a class
-----------------------------------------------------------------------------------------
Example 2:
// class definition with single line comments
// access specifier is optional, class keyword, class name
class My_Program { // start of the class
// statements
// statements
// statements
} // end of a class
------------------------------------------------------------------------------------------
check your knowledge
I hope you have learned something new today!
Now it's time to check your knowledge!!
Let's answer the below queries
Write answers to the following questions in the comments section
- What is a class?
- How many types of comments are there in java and what are they?
- What are access specifiers and how many of them?
- Name all access specifiers in java?
- Are access specifiers are mandatory in java?
- What are the rules for class name?
- Write class definition syntax with comments and without comments?
No comments:
Post a Comment