Linked Questions

-2 votes
1 answer
910 views

Just why are they used? I'm a beginner by the way. I understand default constructors that initialize values because it makes logical sense, but why use a constructor with parameter to say that an ...
Janie's user avatar
  • 29
-1 votes
4 answers
331 views

In the code below is there any advantage/disadvantage/reason for creating the array in the constructor only i.e is there any difference between the two snippets of code below that I should be aware of?...
Danrex's user avatar
  • 1,655
-1 votes
1 answer
589 views

When we can use methods instead of constructor for any operation then what is the use of constructor in java or c++. //Program of Division using constructor:- class Hey { Hey() { int i = ...
Rishabh Chaurasiya's user avatar
1 vote
2 answers
196 views

I did not understand why below code outputs 10 3 times. enum Enums { A, B, C; private Enums() { System.out.println(10); } } public class MainClass { public static void main(...
Dattatraya Shinde's user avatar
0 votes
1 answer
318 views

I know constructor is a special member of class use to initialize data member..But why we actually need constructor can somebody tell me.
Omkar Doke's user avatar
0 votes
0 answers
57 views

I have begun learning OOP in Java, and see no need for constructors. Can't we just assign the values to the variables directly. So what's the need for constructors? What's the difference?
Kalyan Nadimpalli's user avatar
-4 votes
2 answers
162 views

I am very new to C#. Just started learning about OOP and classes. I encountered this class in something I read: public class Thissser { public string whatever; public Thissser(string whatever)...
Mike's user avatar
  • 1
74 votes
10 answers
51k views

What are the pro's and con's of calling out to a mutator from a constructor (if any) i.e.: public MyConstructor(int x) { this.x = x; } versus: public MyConstructor(int x) { setX(x); } public ...
Billworth Vandory's user avatar
89 votes
7 answers
26k views

In other words, why would you need an instance initializer? What difference or advantage do you have in writing a instance initializer over a constructor?
Ajay's user avatar
  • 7,438
7 votes
4 answers
29k views

Let's say we use Java SE (no libraries) and have the following situation. We have a class: public class DriverInfo { private final int age; public DriverInfo(int age) { this.age = ...
Ernestas Kardzys's user avatar
-2 votes
4 answers
10k views

public class Person { private int age; public Person(int initialAge) { if (initialAge<= 0) { System.out.println("Age is not valid, setting age to 0."); } else {...
Zeboon's user avatar
  • 31
3 votes
4 answers
180 views

If a constructor is the only way to create the object of a class then how String name = "Java"; is able to create an object of String class even without using constructor.
JAVA_LOVER's user avatar
1 vote
1 answer
1k views

SO community. I have this small piece of code that I am not sure if it's not filled with bad practices. Basically the email message that i am trying to validate holds some data that i am interested ...
Bobzone's user avatar
  • 386
0 votes
1 answer
696 views

I'm studying Swing in Java at first. above thing is correct and another thing is incorrect code. Correct public class Example extends JFrame{ public Example() { } Incorrect public class ...
Kim ByeongHwa's user avatar
-2 votes
3 answers
210 views

I am trying to calculate the area of the circle using class and object in Java, but the output is not as I want. I want an answer as 78.5 but the area = 0.0, why? Here is the code below- package com....
Vedprakash Maliarya's user avatar

15 30 50 per page