- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
1K+ Views
While declaring the constructors you should keep the following points in mind.A constructor does not have a return type.The name of the constructor is same as the name of the class. A class can have more than one constructor.Examplepublic class Sample { int num; public Sample() { num = 30; } public Sample(int value) { num = value; } }
174 Views
The this is a keyword in Java which is used as a reference to the object of the current class, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods. Example Live Demo public class This_Example { // Instance variable num int num = 10; This_Example() { System.out.println("This is an example program on keyword this"); } This_Example(int num) { ... Read More
2K+ Views
A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error. Example public class Example { } public class Example{ public void sample(){ System.out.println("sample method of the Example class"); } public void demo(){ System.out.println("demo method of the Example class"); } ... Read More
317 Views
This is a static method of the class named Integer it accepts an integer parameter and Parses it as a signed decimal integer. Example Live Demo public class IntegerDemo { public static void main(String[] args) { // parses the string argument int a = Integer.parseInt("12"); int b = Integer.parseInt("26"); int c = Integer.parseInt("54"); int m = a * b * c; System.out.print("Value after multiplying = " + m); } } Output Value after multiplying = 16848
517 Views
The this is a keyword in Java which is used as a reference to the object of the current class. Using it you can − Differentiate the instance variables from local variables if they have same names, within a constructor or a method. Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation. Example class Superclass { int age; Superclass(int age) { this.age = age; } public void ... Read More
3K+ Views
No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors. Example public interface InterfaceTest { public InterfaceTest(){ } public abstract void display(); public abstract void show(); } Still, if you try to write constructors in an interface it will generate a compile time error. Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected InterfaceTest(){ ^ 1 error C:\Sample>
538 Views
No, interfaces can’t have constructors for the following reasons − All the members of an interface are abstract, and since a constructor cannot be abstract. Still, if you try to write a constructor within an interface it will generate a compile time error. Example public interface InterfaceTest { InterfaceTest(){ } public abstract void display(); public abstract void show(); } Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected public InterfaceTest(){ ^ 1 error
366 Views
The super keyword is similar to this keyword. Following are the scenarios where a super keyword is used. It is used to differentiate the members of superclass from the members of the subclass if they have same names. It is used to invoke the superclass constructor from the subclass. Whenever you want to call the constructor of the superclass from a method or another constructor you can do so as: Example class Person { Person(String name) { System.out.println("Hello "+ name); } } class Student ... Read More
243 Views
You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}
2K+ Views
Default constructors in Java:A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. There are two types of constructors namely −parameterized constructors − Constructors with arguments.no-arg constructors − Constructors without arguments.Example Live Demopublic class Sample{ int num; Sample(){ num = 100; } Sample(int num){ this.num = num; } public static void main(String args[]){ System.out.println(new Sample().num); System.out.println(new Sample(1000).num); } }Output100 1000Default ConstructorIt ... Read More