Linked Questions
19 questions linked to/from Purpose of a constructor in Java?
-2
votes
1
answer
910
views
What is the purpose of parameterized constructors? [duplicate]
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 ...
-1
votes
4
answers
331
views
What is the point of a constructor in Java [duplicate]
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?...
-1
votes
1
answer
589
views
Using of methods instead of constructor [duplicate]
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 = ...
1
vote
2
answers
196
views
Why Below Code outputs 10 3 times while I created only single object [duplicate]
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(...
0
votes
1
answer
318
views
Why we use constructor in java [duplicate]
I know constructor is a special member of class use to initialize data member..But why we actually need constructor can somebody tell me.
0
votes
0
answers
57
views
Need For Constructors [duplicate]
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?
-4
votes
2
answers
162
views
Missunderstood the point of a class constructor [duplicate]
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)...
74
votes
10
answers
51k
views
calling setters from a constructor
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 ...
89
votes
7
answers
26k
views
How is an instance initializer different from a constructor?
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?
7
votes
4
answers
29k
views
How to validate constructor parameters on Java?
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 = ...
-2
votes
4
answers
10k
views
Methods with same name as its class? [duplicate]
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 {...
3
votes
4
answers
180
views
Is constructor the only way to create the object of a class in JAVA?
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.
1
vote
1
answer
1k
views
Validating data in constructor and throwing exceptions [duplicate]
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 ...
0
votes
1
answer
696
views
Class name & Function name in Java
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 ...
-2
votes
3
answers
210
views
why class Circle is not working in Java or I made any mistake, Please answer
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....