Skip to main content

UNIT 3 Java

 Unit-3 Class and objects

3.1 class in Java

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

Everything in Java is associated with classes and objects, along with its attributes and methods. A class is container for variable, methods, constructor etc.  For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

Syntax to declare a class:

1.      class <class_name>{  

2.          field;  

3.          method;  

4.      }  

3.2 Object in Java

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. In Java, an object is created from a class.

To create an object of Main, specify the class name, followed by the object name, and use the keyword new:

Example

Create an object called "myObj" and print the value of x:

public class Class_name {

  int x = 5;

  public static void main(String[] args) {

    Class_name myObj = new Class_name();

    System.out.println(myObj.x);

  }

}

We can create multiple objects of one class:

public class Main {

  int x = 5;

  public static void main(String[] args) {

    Main myObj1 = new Main();  // Object 1

    Main myObj2 = new Main();  // Object 2

    System.out.println(myObj1.x);

    System.out.println(myObj2.x);

  }

}

Using Multiple Classes

We can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory/folder:

        Main.java

        Second.java

Main.java

public class Main {

  int x = 5;

}

Second.java

class Second {

  public static void main(String[] args) {

    Main myObj = new Main();

    System.out.println(myObj.x);

  }

}

And the output will be:

5

Modify Attributes

We can also modify attribute values

public class Main {

  int x;

  public static void main(String[] args) {

    Main myObj = new Main();

    myObj.x = 40;

    System.out.println(myObj.x);

  }

}

 

Example

Change the value of x to 25:

public class Main {

  int x = 10;

  public static void main(String[] args) {

    Main myObj = new Main();

    myObj.x = 25; // x is now 25

    System.out.println(myObj.x);

  }

}

If We don't want the ability to override existing values, declare the attribute as final:

Example

public class Main {

  final int x = 10;

  public static void main(String[] args) {

    Main myObj = new Main();

    myObj.x = 25; // will generate an error: cannot assign a value to a final variable

    System.out.println(myObj.x);

  }

}

The final keyword is useful when We want a variable to always store the same value, like PI (3.14159...).

Comments

Popular posts from this blog

Function of OS 1. Process Management The operating system helps in running many programs at the same time. It keeps track of each running program (called a process), decides which one should run next, and stops or starts them as needed. It makes sure that all the programs get a fair chance to use the CPU.   2. Memory Management The OS manages the computer's memory (RAM). It decides which program will use how much memory and keeps track of it. When a program is closed, it frees up the memory so that other programs can use it. This helps the computer run smoothly without crashing.   3. File System Management The operating system helps us to create, save, open, and delete files. It organizes files in folders and keeps them safe. It also controls who can open or edit a file to protect our data.   4. Device Management The OS controls all the input and output devices like the keyboard, mouse, printer, and monitor. It tells the devices what to do and makes su...

UNIT 3 Array in C#

  Array in C# An array is a collection of variables of the same data type , stored in contiguous memory locations , and accessed using a common name and index . Each item in an array is called an element , and the index of arrays in C# starts from 0 .     Key Points About Arrays in C#: 1.       Elements are stored in contiguous memory locations. 2.       Index starts from 0. So, for an array of size 5, valid indexes are 0 to 4. 3.       Arrays are reference types and are allocated on the heap. 4.       C# array is an object of base type System.Array . 5.       Array elements can be of any type , including another array (array of arrays). 6.       Jagged array (array of arrays) elements are reference types and are initialized to null . 7.       Arrays can be single-dimensi...

Unit 2 Java

Data Types A data type defines the kind of data a variable can store, the operations that can be performed on it, and the amount of memory allocated. Java Primitive Data Types Data Type Size Range (Approx.) Example Byte 1 byte -128 to 127 byte a = 100; short 2 bytes -32,768 to 32,767 short s = 1000; Int 4 bytes -2,147,483,648 to 2,147,483,647 int num = 50000; Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long l = 100000L; float 4 bytes ~6–7 decimal digits float f = 5.75f; double 8 bytes ~15 decimal digits double d = 19.99; Char 2 bytes Single Unicode character char c = 'A'; boolean 1 bit* tru...