xxxxxxxxxx
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
xxxxxxxxxx
public class Person {
// class variables
private String name;
private int age;
// constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// other methods
public void sayHello() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
xxxxxxxxxx
class Dog {
int age = 5;
public static void main(String[]args) {
Dog myObj = new Dog();
System.out.println(myObj.age);
}
}
xxxxxxxxxx
Remember from the Java Syntax chapter
that a class should always start with an uppercase first letter,
and that the name of the java file should match the class name.
xxxxxxxxxx
ClassName obj = new ClassName(); //creating an object of ClassName class
System.out.println(obj.field1); // calling field of the object (bad practice, make the fields private)
System.out.println(obj.field2);
obj.doSomething(); // calling method of the object
xxxxxxxxxx
It is a basic unit of Object-Oriented Programming and represents real life
entities. A typical Java program creates many objects, which as you know,
interact by invoking methods. An object consists of :
State: It is represented by attributes of an object. It also reflects the
properties of an object.
Behavior: It is represented by methods of an object. It also reflects the
response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to
interact with other objects.
class Student
{
int id;//data member (also instance variable)
String name; //data member (also instance variable)
public static void main(String args[])
{
Student s1=new Student();//creating an object of Student
System.out.println(s1.id);
System.out.println(s1.name);
}
}
xxxxxxxxxx
// Default behavior of toString() is to print class name, then
// @, then unsigned hexadecimal representation of the hash code
// of the object
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
xxxxxxxxxx
The Object class is the parent class of all the classes in java by default.In
other words, it is the topmost class of java.
The Object class is beneficial if you want to refer any object whose type you
dont know. Notice that parent class reference variable can refer the child
class object, know as upcasting.
xxxxxxxxxx
// Example : TestClass (Can be predefined or user-defined)
public class TestClass {
// properties
private int id = 111;
// constructor
public TestClass(){
super();
}
// method
public void test(){
// some code here
}
}