Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 17 of 50

Can we declare main() method as private or protected or with no access modifier in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 7K+ Views

Java provides various access specifiers namely private, public and protected etc...The Private modifier restricts the access of members from outside the class. A class and interface cannot be public.The Public access modifier can be associated with class, method, constructor, interface, etc. public can be accessed from any other class.The protected access modifier can be associated with variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The default access modifier does not have keyword a variable or method declared ...

Read More

Can We declare main() method as Non-Static in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 8K+ Views

The public static void main(String ar[]) method is the entry point of the execution in Java. When we run a .class file JVM searches for the main method and executes the contents of it line by line.You can write the main method in your program without the static modifier, the program gets compiled without compilation errors. But, at the time of execution JVM does not consider this new method (without static) as the entry point of the program.  It searches for the main method which is public, static, with return type void, and a String array as an argument.public static int main(String[] ...

Read More

Can we declare final variables without initialization in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 5K+ Views

In Java, final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them.Still, if you try to declare final variables without initialization that will generate a ...

Read More

How many ways are there to initialize a final variable in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 3K+ Views

In Java, final is the access modifier which can be used with a filed, class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Initializing a final variableOnce you declare a final variable, it is a must to initialize it. You can initialize the final instance variable −At the time of declaration as.public final String name = "Raju"; public final int age = 20;Within an instance (non-static) block.{    this.name = "Raju";    this.age = 20; }Within a default constructor.public final ...

Read More

Why final variable doesn't require initialization in main method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 823 Views

In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t, a compile-time error is generated.Example Live Demopublic class Student {    public final String name;    public final int age;    public void display(){       System.out.println("Name of the Student: "+this.name); ...

Read More

What happens if we call "super()" in a constructor without extending any class, in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 3K+ Views

A super keyword is a reference of the superclass object in Java. Using this, you can invoke the instance methods constructors and, variables, of a superclass.Calling the constructor of a superclassYou can call the constructor of a superclass from the subclass’s constructor.ExampleConsider the following example, it has two classes SuperClass class and SubClass where the SubClass extends the SuperClass. The superclass has a parameterized constructor, we are invoking it from the subclass using the "super" keyword. Live Democlass SuperClass{    SuperClass(int data){       System.out.println("Superclass's constructor: "+data);    } } public class SubClass extends SuperClass{    SubClass(int data) {       ...

Read More

Can we call Superclass’s static method from subclass in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 3K+ Views

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.Example Live Demopublic class Sample{    public static void display(){       System.out.println("This is the static method........");    }    public static void main(String args[]){       Sample.display();    } }OutputThis is the static method........It also works, if you call a static method using an instance. But, it is not recommended. Live Demopublic class Sample{    public static void display(){       System.out.println("This is the static ...

Read More

Is it possible to assign a reference to "this" in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 1K+ Views

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using it, you can refer the members of a class such as constructors, variables, and methods.Assigning reference to "this"According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.Still, if you try to it assign a reference value to "this" it ...

Read More

Can we return this keyword from a method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 7K+ Views

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods.Returning “this”Yes, you can return this in Java i.e. The following statement is valid.return this;When you return "this" from a method the current object will be returned.ExampleIn the following Java example, the class Student has two private variables name and age. From a method setValues() we are reading values from user and assigning them to these (instance) variables and returning the current object.public class Student ...

Read More

How to remove the tick marks in JavaFX XY charts?

Maruthi Krishna
Maruthi Krishna
Updated on 20-May-2020 361 Views

The javafx.scene.XYChart class is the base class of all the charts that are plotted in an x-y pane. By instantiating the subclasses of this class you can create various XY charts namely − line chart, area chart, bar chart, pie chart, bubble chart, scatter chart, etc.In the XY chart, the given data points are plotted on an XY plane. Along the x and y axes, you will have the tick marks and tick labels. The tick marks represent various values with uniform intervals.Removing the tick marksThe javafx.scene.chart.Axis class (abstract) is the base class of all the axes in XY charts. ...

Read More
Showing 161–170 of 500 articles
« Prev 1 15 16 17 18 19 50 Next »
Advertisements