0% found this document useful (0 votes)
15 views

java

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

java

Uploaded by

Swati Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

If any static variable is final, we can not initialize it even in constructor.

We can initialize final variable at time of and declaration and in constructor, not
that the final variable should to static.
Recommended way of declaring constant:
Final int LOG_IN_TIME= 10;
Enum with switch:
Super keyword:
We cannot use private and protect access modifier with class.

Method hiding is possible with static method.


We can not override static method.

Method hiding:
In above example we are accessing static method using class name not object,
called method hiding.
Using throws we are postponing exception to its calling method.
Below exception is not handled as we used throws:

To handle it we use try catch.


The program is exit with 0 means success, (handled exception).

Java – Try with


Resources
1. Overview
Support for try-with-resources — introduced in Java 7 — allows us
to declare resources to be used in a try block with the assurance
that the resources will be closed after the execution of that block.
The resources declared need to implement
the AutoCloseable interface.
2. Using try-with-resources
Simply put, to be auto-closed, a resource has to be both declared
and initialized inside the try:
try (PrintWriter writer = new PrintWriter(new File("test.txt"))) {
writer.println("Hello World");
}
Copy

3. Replacing try–catch-finally With try-


with-resources
The simple and obvious way to use the new try-with-
resources functionality is to replace the traditional and
verbose try-catch-finally block.
Let’s compare the following code samples.
The first is a typical try-catch-finally block:
Scanner scanner = null;
try {
scanner = new Scanner(new File("test.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}Copy
And here’s the new super succinct solution using try-with-
resources:
try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
If parent class has paramerized constructor, then its child class must have
parametrized consutror. Note that constructor with parameter will not work
give error because before child class, parent class consturtor called, and
parameter of paramerized constror of parenet class does not pass from child
class therefore both constructor should be parametrixed.
Not that extra parameters in child constructor are also fine.

You might also like