String name = "Deepak";
// here name is identifierint rollno = 101;
// here rollno is identifierclass Test {-}
// here Test is identifierint myVariable;
String userName;
int my Variable; // Error: Spaces are not allowed
String user Name; // Error: Spaces are not allowed
_
and $
) can be used in an identifier.
_
) or dollar signs ($
), but other special symbols like @
, #
or !
are not allowed.
int _counter;
double $price;
String first_name;
int user#name; // Error: '#' is not allowed
double total@; // Error: '@' is not allowed
int variable1;
double _123value;
int 1variable; // Error: Cannot start with a digit
double 3value; // Error: Cannot start with a digit
class
, public
, if
, etc.) cannot be used as identifiers.
int myClass;
String _if;
int class; // Error: 'class' is a reserved keyword
String public; // Error: 'public' is a reserved keyword
MyVariable
and myVariable
are treated as distinct identifiers.
int MyVariable = 10;
int myVariable = 20; // Different from 'MyVariable'
int thisIsAnExtremelyLongVariableName = 100; // Valid but not recommended
int println = 5; // Valid but confusing, as 'println' is used in System.out.println()
int x = 10; // Valid but not descriptive
int age = 10; // Better: Descriptive and meaningful
Naming | Single Word | Two Words | Three Words |
---|---|---|---|
Classes, Interfaces | Example |
MyExample |
MyExampleDemo |
Methods | example() |
myExample() |
myExampleDemo() |
Variables | example |
my_example |
my_example_demo |
Constants | EXAMPLE |
MY_EXAMPLE |
MY_EXAMPLE_DEMO |
Packages | example |
my.example |
my.example.demo |
MyClass
, UserAccount
.
getDetails()
, calculateTotal()
.
user_age
, total_price
.
MAX_VALUE
, DEFAULT_TIMEOUT
.
com.example.project
.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.