Posts

Showing posts from February, 2023

Constructor in Java

  Constructor: 1. It is used to initialize an object. 2. It is open type of special method. 3. It is used to provide values for the "instance" variables. 4. Constructor is invoked implicitly. 5. The java compiler provides a default constructor if you don't have any Constructor. Rules: 1. Constructor should be having same name as class name. 2. It should not having any return type. Syntax: <Access Modifier><class name>() Two types of constructor: 1. Default Constructor:          Constructor which has no parameters,known as Default Constructor. 2. Parameterized Constructor :          Constructor which has a parameters, known as Parameterized constructor.          Constructor will be called simultaneously whenever we are creating an object. Keypoints in Java Constructor: 1. We are supposed to initialize an object with the available constructor present in the class. 2. If we are trying to initialize ...

Data types and Variables

Image
  Datatype:     Specifies the size and then type of values that can be stored in an identifier.     They are used to represent how much memory is required to hold the data. Variables:      Variables are nothing but a name given to storage area. Three types of variables:      1. Local Variable      2. Instance variable      3. Static variable 1. Local Variable:   A  local variable  in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists. If we are declaring a local variable then we should initialize it within the block before using it . 2. Instance variable: The variables which are declared inside a class outside any method /block/ a constructor are known as instance variables.       *  For Instance variables JVM will automatically ass...

Basics about Core Java

Image
  Core Java -   Three basic elements in Java: 1) Class:   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. A class in Java can contain: Fields Methods Constructors Blocks Nested class and interface 2) Interface:   An   interface in Java   is a blueprint of a class. It has static constants and abstract methods. The interface in Java is  a mechanism to achieve  abstraction . There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. 3). Enum:   An enum type is  a special data type that enables for a variable to be a set of predefined constants . The variable must be equal to one of the values that have been predefined ...