BZC Core Java Notes Part - 9
BZC Core Java Notes Part - 9
- When you pass primitive values as arguments to a method, they are passed by value.
- copy of the actual value of the argument is passed to the method rather than the original variable
itself.
Pass by reference
- When you pass an object to a method in Java, you are passing a copy of the reference (memory
address) to that object.
- Since the reference points to the same object in memory, modifications made to the object's state
inside the method are reflected outside the method as well.
b) Constructor Chaining
Constructor chaining refers to the process of calling one constructor from
another constructor within the same class using the this() keyword.
If present it must be the first statement inside a constructor.
Call to a constructor using this() must be made inside only another constructor.
Syntax:
this([arguments]); arguments optional
Example:
this(); calls no-arg (D.C) constructor
this(12); calls one arg constructor which takes integer as parameter.
this(12,13); calls 2-arg cons which takes 2 integers as parameters.
Class Declaration:
- The file which contains programs are also known as source files.
- And the code written inside source file is also known as source code.
- A java source file mostly contains classes or interfaces.
- Following are the rules pertaining to java source files:
a) A java source file can contain any number of non-public classes or interfaces.
b) A java file can contain at most one public class.
c) If it contains one public class the source file name should be same as public class name.
d) If it does not contain any public class then we can give any name to source file.
Compiling and running java source file from CLI (Command Line Interface):
BZ_Coder_Training
Java_programs
A.java
BZ_Coder_Training
Java_programs
Packages:
- Packages are related classes grouped together in a single unit.
- Packages organize classes and interfaces into different namespaces, preventing naming conflicts by
allowing classes with the same name to coexist as long as they are in different packages.
- To use class from another package we use fully qualified class name.
- Fully Qualified Classname = packagename.classname
- To be accessible from another package a class and its members must be declared as public.
- To declare that a Java class belongs to a specific package, the package statement is used at the
beginning of the source file.
Syntax:
package <[.separated]pkgName>;
- If we don’t provide package statement then the class is considered to be in default package.
- Packages are generated as directories, but they are not directories.
Importing packages
- When we have use classes from one package to another package we give fully-qualified class name.
- If we have to use those classes at many places in our code then the readability of the program is
reduced and writing fully qualified class names can becomes cumbersome and time consuming too.
- To improve readability, and reduce typing effort import statements are used.
- import statement is used to access classes and interfaces from other packages.
- It simplifies code by allowing you to reference classes without typing their fully qualified names
every time you use them.
- If a source file contains package statement, then import statements must be the next statement
after it.
- If a source file does not contain package statement, then import statements must be written first.
Syntax:
import packagename.classname; imports only one class
import packagename.*; imports all the classes from the package