Date: 25-04-2024
________________
How to create a class in Java?
------------------------------------------
File >> New >> JavaProject (ProjectName)
| - src >> Package (PackageName) >> Class
(ClassName)
| - JRE Library
Identifiers:
----------------
The naming in Java is called as identifiers
Different types of namings in Java:
-------------------------------------------------
-> Project Name
-> Package Name
-> Class Name
-> Method Name
-> Variable Name
-> Object Name
Restrictions to create the naming in Java:
----------------------------------------------------------
The only allowed characters to create namings are
-> A-Z (Uppercase Alphabets)
-> a-z (Lowercase Alphabets)
-> 0-9 (digits)
-> $ (Dollar)
-> _ (Underscore)
-> eg: int ___ = 10; ---> Allowed
int $=4; ---> Allowed
int 0aco=1; ---> Not allowed (names should not start with
numbers)
int a@bc=0; ---> Not allowed
int aA_$___002=3; ---> Allowed
How to create class structure in Java?:
------------------------------------------------------
class Sample
{
// we can create methods, variables, objects constructor, blocks inside a
class
}
How to create method in Java?:
---------------------------------------------
-> Method should contain one method name
-> It ends with parameter brackets()
-> It contains return types
-> It is enclosed with curly brackets or block
There are two return types in Java:
--------------------------------------------------
-> void return type
-> primitive and non-primitive return type
void return type:
------------------------
public void method(){
int a = 10;
}
primitive return type:
-------------------------------
public int method(){
int b = 20;
return b;
}
non-primitive return type:
-------------------------------------
public Sample method(){
String obj = new String("hello");
return obj;
}
___________________________________________________________________________________
________