Question stringlengths 1 113 | Answer stringlengths 22 6.98k |
|---|---|
What is Java Enum in Switch Statement? | Java allows us to use enum in switch statement. Java enum is a class that represent the group of constants. (immutable such as final variables). We use the keyword enum and put the constants in curly braces separated by comma.
Example:
JavaSwitchEnumExample.java
//Java Program to demonstrate the use of Enum
//in s... |
What is Java Wrapper in Switch Statement? | Java allows us to use four wrapper classes: Byte, Short, Integer and Long in switch statement.
Example:
WrapperInSwitchCaseExample.java
//Java Program to demonstrate the use of Wrapper class
//in switch statement
public class WrapperInSwitchCaseExample {
public static void main(String args[])
... |
What is Loops in Java? | The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loops in Java.
-Simple for Loop
-For-each or Enhanced for Loop
-Labeled for Loop
|
Can you provide an example of Java Simple for Loop? | A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable.... |
What is Java Nested for Loop? | If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example:
NestedForExample.java
public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(i... |
What is Java for-each Loop?
| The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on the basis of elements and not the index. It returns element one by one in the defined variable.
Syntax:
for(data_type vari... |
What is Java Labeled For Loop? | We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop.
Syntax:
labelname:
for(initialization; condition; increment/decrement){
//code to be executed
}
Example:
LabeledForE... |
What is Java Infinitive for Loop? | If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
for(;;){
//code to be executed
}
Example:
ForExample.java
//Java program to demonstrate the use of infinite for loop
//which prints an statement
public class ForExample {
public static void main(Strin... |
Can you explain the differences between the Java for loop, while loop, and do-while loop? | Java for Loop vs while Loop vs do-while Loop
For loop
Introduction : The Java for loop is a control flow statement that iterates a part of the programs multiple times.
When to use : If the number of iteration is fixed, it is recommended to use for loop.
Syntax: for(init;condition;incr/decr){
// code to be executed
}
E... |
What is Java While Loop? | The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to ... |
What is Java Infinitive While Loop? | If you pass true in the while loop, it will be infinitive while loop.
Syntax:
while(true){
//code to be executed
}
Example:
WhileExample2.java
public class WhileExample2 {
public static void main(String[] args) {
// setting the infinite while loop by passing true to the condition
while(true){ ... |
What is Java do-while loop?
| The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop.
Java do-while loop is called an exit control loop. Therefore, unlike ... |
What is Java Infinitive do-while Loop? | If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
do{
//code to be executed
}while(true);
Example:
DoWhileExample2.java
public class DoWhileExample2 {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop"); ... |
What is Java Break Statement? | When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop... |
Can you provide an example of Java Break Statement with Loop? | Example:
BreakExample.java
//Java Program to demonstrate the use of break statement
//inside the for loop.
public class BreakExample {
public static void main(String[] args) {
//using for loop
for(int i=1;i<=10;i++){
if(i==5){
//breaking the loop
b... |
What is Java Break Statement with Inner Loop? | It breaks inner loop only if you use break statement inside the inner loop.
Example:
BreakExample2.java
//Java Program to illustrate the use of break statement
//inside an inner loop
public class BreakExample2 {
public static void main(String[] args) {
//outer loop
for(i... |
Can you provide an example of Java Break Statement with Labeled For Loop? | We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.
Example:
BreakExample3.java
//Java Program to illustrate the use of continue statement
//with label inside an inner loop to break outer loop
public cl... |
Can you provide an example of Java Break Statement in while loop? | BreakWhileExample.java
//Java Program to demonstrate the use of break statement
//inside the while loop.
public class BreakWhileExample {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=10){
if(i==5){
//using break statement
... |
Can you provide an example of Java Break Statement in do-while loop? | BreakDoWhileExample.java
//Java Program to demonstrate the use of break statement
//inside the Java do-while loop.
public class BreakDoWhileExample {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
//us... |
What is Java Continue Statement? | The continue statement is used in loop control structure when you need to jump to the next iteration of the loop immediately. It can be used with for loop or while loop.
The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specifie... |
Can you provide an example of Java Continue Statement Example? | ContinueExample.java
//Java Program to demonstrate the use of continue statement
//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it w... |
Can you provide an example of Java Continue Statement with Inner Loop? | It continues inner loop only if you use the continue statement inside the inner loop.
ContinueExample2.java
//Java Program to illustrate the use of continue statement
//inside an inner loop
public class ContinueExample2 {
public static void main(String[] args) {
//outer loop
fo... |
Can you provide an example of Java Continue Statement with Labelled For Loop? | We can use continue statement with a label. This feature is introduced since JDK 1.5. So, we can continue any loop in Java now whether it is outer loop or inner.
Example:
ContinueExample3.java
//Java Program to illustrate the use of continue statement
//with label inside an inner loop to continue outer loop... |
Can you provide an example of Java Continue Statement in while loop? | ContinueWhileExample.java
//Java Program to demonstrate the use of continue statement
//inside the while loop.
public class ContinueWhileExample {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=10){
if(i==5){
//using continue statement ... |
Can you provide an example of Java Continue Statement in do-while Loop? | ContinueDoWhileExample.java
//Java Program to demonstrate the use of continue statement
//inside the Java do-while loop.
public class ContinueDoWhileExample {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
... |
What is Java Comments? | The Java comments are the statements in a program that are not executed by the compiler and interpreter.
|
Why do we use comments in a code? | -Comments are used to make the program more readable by adding the details of the code.
-It makes easy to maintain the code and to find the errors easily.
-The comments can be used to provide information or explanation about the variable, method, class, or any statement.
-It can also be used to prevent the execution... |
What are the Types of Java Comments? | There are three types of comments in Java.
1) Single Line Comment
2 ) Multi Line Comment
3) Documentation Comment
|
What is Java Single Line Comment? | The single-line comment is used to comment only one line of the code. It is the widely used and easiest way of commenting the statements.
Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java.
Syntax:
//This is single line comment
Let's use single line ... |
What is Java Multi Line Comment? | The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line comments there).
Multi-line comments are placed between /* and */. Any text between /* and */ is not executed b... |
What is Java Documentation Comment? | Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods, arguments, etc., are used in the code.
To create documentation API, we need to use the javadoc tool. The do... |
What is javadoc tags? | javadoc tags
Some of the commonly used tags in documentation comments:
Tag: {@docRoot}
Syntax: {@docRoot}
Description: to depict relative path to root directory of generated document from any page.
Tag: @author
Syntax: @author name - text
Description: To add the author of the class
Tag: @code
Syntax: {@c... |
Are Java comments executable?
| As we know, Java comments are not executed by the compiler or interpreter, however, before the lexical transformation of code in compiler, contents of the code are encoded into ASCII in order to make the processing easy.
Test.java
public class Test{
public static void main(String[] args) {
//the... |
Can you provide a list of Java Programs | Java Programming Examples? | Java programs are frequently asked in the interview. These programs can be asked from control statements, array, string, oops etc. Java basic programs like fibonacci series, prime numbers, factorial numbers and palindrome numbers are frequently asked in the interviews and exams. All these programs are given with the ma... |
Can you provide a list of Java Basic Programs? | 1) Fibonacci Series in Java
2) Prime Number Program in Java
3) Palindrome Program in Java
4) Factorial Program in Java
5) Armstrong Number in Java
6) How to Generate Random Number in Java
7) How to Print Pattern in Java
8) How to Compare Two Objects in Java
9) How to Create Object in Java
10) How to Print ASCI... |
Can you provide a list of Java Number Programs? | 1) How to Reverse a Number in Java
2) Java Program to convert Number to Word
3) Automorphic Number Program in Java
4) Peterson Number in Java
5) Sunny Number in Java
6) Tech Number in Java
7) Fascinating Number in Java
8) Keith Number in Java
9) Neon Number in Java
10) Spy Number in Java
11) ATM program Java
... |
Can you provide a list of Java Array Programs? | 1) Java Program to copy all elements of one array into another array
2) Java Program to find the frequency of each element in the array
3) Java Program to left rotate the elements of an array
4) Java Program to print the duplicate elements of an array
5) Java Program to print the elements of an array
6) Java Progr... |
Can you provided a list of Java Matrix Programs? | 1) Java Matrix Programs
2) Java Program to Add Two Matrices
3) Java Program to Multiply Two Matrices
4) Java Program to subtract the two matrices
5) Java Program to determine whether two matrices are equal
6) Java Program to display the lower triangular matrix
7) Java Program to display the upper triangular matri... |
Can you provide a list of Java String Programs? | 1) Java Program to count the total number of characters in a string
2) Java Program to count the total number of characters in a string 2
3) Java Program to count the total number of punctuation characters exists in a String
4) Java Program to count the total number of vowels and consonants in a string
5) Java Prog... |
Can you provide a list of Java Searching and Sorting Programs? | 1) Linear Search in Java
2) Binary Search in Java
3) Bubble Sort in Java
4) Selection Sort in Java
5) Insertion Sort in Java |
Can you provide a list of Java Conversion Programs? | 1) How to convert String to int in Java
2) How to convert int to String in Java
3) How to convert String to long in Java
4) How to convert long to String in Java
5) How to convert String to float in Java
6) How to convert float to String in Java
7) How to convert String to double in Java
8) How to convert double... |
Can you provide a list of Java Pattern programs? | 1) Java program to print the following spiral pattern on the console
2) Java program to print the following pattern
3) Java program to print the following pattern 2
4) Java program to print the following pattern 3
5) Java program to print the following pattern 4
6) Java program to print the following pattern 5
7)... |
Can you provide a list of Java Singly Linked List Programs? | 1) Singly linked list Examples in Java
2) Java Program to create and display a singly linked list
3) Java program to create a singly linked list of n nodes and count the number of nodes
4) Java program to create a singly linked list of n nodes and display it in reverse order
5) Java program to delete a node from th... |
Can you provide a list of Java Circular Linked List Programs? | 1) Java program to create and display a Circular Linked List
2) Java program to create a Circular Linked List of N nodes and count the number of nodes
3) Java program to create a Circular Linked List of n nodes and display it in reverse order
4) Java program to delete a node from the beginning of the Circular Linked... |
Can you provide a list of Java Doubly Linked List Programs? | 1) Java program to convert a given binary tree to doubly linked list
2) Java program to create a doubly linked list from a ternary tree
3) Java program to create a doubly linked list of n nodes and count the number of nodes
4) Java program to create a doubly linked list of n nodes and display it in reverse order
5)... |
Can you provide a list of Java Tree Programs? | 1) Java Program to calculate the Difference between the Sum of the Odd Level and the Even Level Nodes of a Binary Tree
2) Java program to construct a Binary Search Tree and perform deletion and In-order traversal
3) Java program to convert Binary Tree to Binary Search Tree
4) Java program to determine whether all le... |
What is the difference between an object-oriented programming language and object-based programming language? | Object-based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object-based programming languages.
|
What is Java Naming Convention? | Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc.
But, it is not forced to follow. So, it is known as convention not rule. These conventions are suggested by several Java communities such as Sun Microsystems and Netscape.
... |
What is the Advantage of Naming Conventions in Java? | By using standard Java naming conventions, you make your code easier to read for yourself and other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
|
What are the Naming Conventions of the Different Identifiers? | Naming Conventions of the Different Identifiers
The following table shows the popular conventions used for the different identifiers.
Identifiers Type: Class
Naming Rules: It should start with the uppercase letter.
It should be a noun such as Color, Button, System, Thread, etc.
Use appropriate words, instead o... |
What is CamelCase in Java naming conventions? | Java follows camel-case syntax for naming the class, interface, method, and variable.
If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc. |
What is Objects and Classes in Java? | In this page, we will learn about Java objects and classes. In object-oriented programming technique, we design a program using objects and classes.
An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only. |
What is an object in Java? | An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
An object has three characteristics:
-State: represents the data (value) of an object.
-Behavio... |
What is a 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.
A class in Java can contain:
-Fields
-Methods
-Constructors
-Blocks
-Nested class and interface |
What is Instance variable in Java? | A variable which is created inside the class but outside the method is known as an instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when an object or instance is created. That is why it is known as an instance variable. |
What is Method in Java? | In Java, a method is like a function which is used to expose the behavior of an object.
Advantage of Method
-Code Reusability
-Code Optimization |
What is new keyword in Java? | The new keyword is used to allocate memory at runtime. All objects get memory in Heap memory area. |
Can you provide an example of Object and Class Example: main within the class in Java? | In this example, we have created a Student class which has two data members id and name. We are creating the object of the Student class by new keyword and printing the object's value.
Here, we are creating a main() method inside the class.
//Java Program to illustrate how to define a class and fields
//Defining ... |
Can you provide an example of Object and Class Example: main outside the class in Java? | In real time development, we create classes and use it from another class. It is a better approach than previous one. Let's see a simple example, where we are having main() method in another class.
We can have multiple classes in different Java files or single Java file. If you define multiple classes in a single Ja... |
What are the 3 Ways to initialize object? | There are 3 ways to initialize object in Java.
-By reference variable
-By method
-By constructor |
Can you provide an example of Object and Class Example: Initialization through reference in Java? | 1) Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Let's see a simple example where we are going to initialize the object through a reference variable.
class Student{
int id;
String name;
}
class TestStudent2{
public static void ma... |
Can you provide an example of Object and Class Example: Initialization through method in Java? | 2) Object and Class Example: Initialization through method
In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by invoking the displayInformation() method.
class Studen... |
Can you provide an example of Object and Class Initialization through a constructor in Java? | We will learn about constructors in Java later.
Let's see an example where we are maintaining records of employees.
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void displ... |
What are the different ways to create an object in Java? | There are many ways to create an object in java. They are:
-By new keyword
-By newInstance() method
-By clone() method
-By deserialization
-By factory method etc. |
What is Anonymous object? | Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only.
If you have to use an object only once, an anonymous object is a good approach.
For example:
new Calculation();//anonymous object
Calling method through a r... |
How to create multiple objects by one type only in Java? | We can create multiple objects by one type only as we do in case of primitives.
Initialization of primitive variables:
int a=10, b=20;
Initialization of refernce variables:
Rectangle r1=new Rectangle(), r2=new Rectangle();//creating two objects
Let's see the example:
//Java Program to illustrate th... |
What is a method in Java? | A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the reusability of code. We write a method once and use it many times. We do not require to write code again and again. It also provides the easy modification and reada... |
What is Method Declaration? | The method declaration provides information about method attributes, such as visibility, return-type, name, and arguments. It has six components that are known as method header.
Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter li... |
How to name a Method? | While defining a method, remember that the method name must be a verb and start with a lowercase letter. If the method name has more than two words, the first name must be a verb followed by adjective or noun. In the multi-word method name, the first letter of each word must be in uppercase except the first word. For e... |
What are the Types of Method? | There are two types of methods in Java:
-Predefined Method
-User-defined Method
|
What is Predefined Method? | In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point. Some pre-defined methods are length(), eq... |
What is User-defined Method? | The method written by the user or programmer is known as a user-defined method. These methods are modified according to the requirement.
How to Create a User-defined Method
Let's create a user defined method that checks the number is even or odd. First, we will define the method.
//user defined method
public st... |
What is Static Method? | A method that has static keyword is known as static method. In other words, a method that belongs to a class rather than an instance of a class is known as a static method. We can also create a static method by using the keyword static before the method name.
The main advantage of a static method is that we can call... |
What is Instance Method? | The method of the class is known as an instance method. It is a non-static method defined in the class. Before calling or invoking the instance method, it is necessary to create an object of its class. Let's see an example of an instance method.
InstanceMethodExample.java
public class InstanceMethodExample
{ ... |
There are two types of instance method? | There are two types of instance method:
-Accessor Method
-Mutator Method
|
What is Accessor Method in Java? | The method(s) that reads the instance variable(s) is known as the accessor method. We can easily identify it because the method is prefixed with the word get. It is also known as getters. It returns the value of the private field. It is used to get the value of the private field
.
Example:
public int getId()
... |
What is Mutator Method in Java? | The method(s) read the instance variable(s) and also modify the values. We can easily identify it because the method is prefixed with the word set. It is also known as setters or modifiers. It does not return anything. It accepts a parameter of the same data type that depends on the field. It is used to set the value o... |
Can you provide an Example of accessor and mutator method? | Student.java
public class Student
{
private int roll;
private String name;
public int getRoll() //accessor method
{
return roll;
}
public void setRoll(int roll) //mutator method
{
this.roll = roll;
}
public String getName()
{
return name;
}
public void setName(Str... |
What is Abstract Method in Java? | The method that does not has method body is known as abstract method. In other words, without an implementation is known as abstract method. It always declares in the abstract class. It means the class itself must be abstract if it has abstract method. To create an abstract method, we use the keyword abstract.
Synta... |
Can you provide an Example of abstract method? | Demo.java
abstract class Demo //abstract class
{
//abstract method declaration
abstract void display();
}
public class MyClass extends Demo
{
//method impelmentation
void display()
{
System.out.println("Abstract method?");
}
public static void main(String args[])
{
//creatin... |
What is Factory method in Java? | It is a method that returns an object to the class to which it belongs. All static methods are factory methods. For example, NumberFormat obj = NumberFormat.getNumberInstance();
|
What is Constructors in Java? | In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created usin... |
What are the Rules for creating Java constructor? | There are two rules defined for the constructor.
1) Constructor name must be the same as its class name
2) A Constructor must have no explicit return type
3) A Java constructor cannot be abstract, static, final, and synchronized
|
What are theTypes of Java constructors? | There are two types of constructors in Java:
1) Default constructor (no-arg constructor)
2) Parameterized constructor
|
What is the Java Default Constructor? | A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
<class_name>(){}
|
Can you provide an Example of default constructor? | In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public... |
Can you provide an Example of default constructor that displays the default values? | //Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects
Student3 s1... |
What is Java Parameterized Constructor? | A constructor which has a specific number of parameters is called a parameterized constructor.
Why use the parameterized constructor?
The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
|
Can you provide an Example of parameterized constructor in Java? | In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
//Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constr... |
What is Constructor Overloading in Java? | In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are di... |
Can you provide an Example of Constructor Overloading? | //Java program to overload constructors
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
... |
What is Java Copy Constructor? | There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
-By constructor
-By assigning the values of one object into another
-By clone() method of Object cla... |
How to copy values without constructor in Java? | We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void d... |
What is Java static keyword? | The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.
The static can be:
1) Variable (also known as a class variable)
2) Method (also known as a class ... |
What is Java static variable? | If you declare any variable as static, it is known as a static variable.
-The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
-The static variable gets memory only once in the... |
Can you provide an Example of static variable in Java? | //Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
... |
Can you provide an example Program of the counter without static variable in Java? | In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable. If it is incremented, it won't reflect other objects. So each object will have the v... |
Can you provid an example Program of counter by static variable in Java? | static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
//Java Program to illustrate the use of static variable which
//is shared with all objects.
class Counter2{
static int count=0;//will get memory only once and retain its value
C... |
What is Java static method? | If you apply static keyword with any method, it is known as static method.
-A static method belongs to the class rather than the object of a class.
-A static method can be invoked without the need for creating an instance of a class.
-A static method can access static data member and can change the value of it. |
Can you provide an Example of static method in Java? | //Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
}
//constructor to initialize the var... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.