Why call to " this " must be first statement in constructor? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to execute this below code sample to understand why call to " this " must be first statement in constructor ?? I had read lot of About it and I understand Why this is so !!
so I write the below simple program But Still showing me the same Error even I use 'this' as a First Statement in my Program .
import java.io.*;
import java.lang.*;
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
class ThisDemo
{
public static void main(String []args)
{
Demo obj = new Demo();
}
}

To specifically answer your question, this or super needs to be the first call to ensure the base class has been setup correctly.
https://stackoverflow.com/a/1168356/154186
To solve your error above, remove the void type from the function call. e.g.:
Demo(int x) {
this.x = x;
}
Demo() {
this(50);
}

Remove void from Demo constructors
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}

You should remove void.Constructor must have no explicit return type.
Then it will work fine.

Related

this code of mine has no error but it isnt showing any output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In this code the method setEid isn't working dont know what's the problem with while loop
this code of mine has no error but it isnt showing any output
public class Emp {
private String eid;
public String getEid() {
return eid;
}
public void setEid(String e) {
while (e.length() < 12) {
eid = e;
}
}
}
public class Test {
public static void main(String[] args) {
Emp z = new Emp();
z.setEid("rgrge");
System.out.println("\n" + z.getEid());
}
}
enter code here i expect the static initialization of setEid argument should not take more than 12 characters
Your setter method have a while loop if eid length is less than 12 then it always stuck in this method.
public void setEid(String e){
while(e.length()<12)
eid=e;
}
and In your main method
public static void main(String[] args) {
Emp z=new Emp();
z.setEid("rgrge"); // you call setter
System.out.println("\n"+z.getEid());
}
you pass "rgrge" in setter and its length is less than 12. Tht's why your program is stuck in loop and not showing any thing.
Change setter implementation to this.
public void setEid(String e){
if(e.length()<12) // change while to if
eid=e;
}
so first there should have had if instead of while.
Change:
while(e.length()<12) change this as if(e.length()<12)
Explanation:
your setEid method never going to run because there is no break condition there. your while loop condition tends to the infinite. your code gets stuck in that while loop.
here you passed z.setEid("rgrge");. in your example, Length of string e is 5 which is obviously less than 12, so the condition will always be satisfied and your program will be stuck in infinite loop.

Detect the programming language [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can anyone please tell me what is this coding language? It has similar structure to Java but I cannot understand.
class Sample {
String student;
#Override
public String toString() {
return "${student}";
}
public static void main(String[] args) {
Sample s = new Sample();
s.student = "Joe"
System.out.println(s.toString())
}
}
This piece of code is valid in groovy... and print "Joe" on standard output.
It is Java, no discussion on this.
But 2 errors :
class Sample {
String student;
#Override
public String toString() {
return student; //<------------------ and not "${student}"
}
public static void main(String[] args) {
s.student = "Joe"; //<---------------- don't forget ';'
}
}
First error is intuitive, I mean you can return "${student}; it will print it, but it's not what the user wanted to do, quite sure

Why is it showing ";" expected in main statement while compiling? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Not able to compile and it is showing ; expected at the end of public static statement.
//Recursive program
class Recur
{
public static void main(String args[])
void witty(String n, int p)
{
if(p<0)
System.out.println("");
else
{
System.out.println(n.charAt(p)+".");
witty(n, p-1);
System.out.print(n.charAt(p));
}
}
}
It seems that you forgot the body of the main method:
public static void main(String args[]) {
}
Note that the body of a method is delimited by { and }.

Why does this array initializer throw a NullPointerException? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am studying Java currently, and I am wondering why this code throws a NullPointerException at the line indicated below. The Question object is a user-defined class that takes two strings as parameters when initializing the object.
public class QuizTime
{
public static void main (String[] args)
{
Quiz qz = new Quiz();
// Throws a NullPointerException
qz.add (new Question ("How may US states are there?", "50"));
}
}
Below is the supporting class. The NullPointerException also indicates a problem with the line "quiz[count] = q;"
import java.util.Scanner;
public class Quiz
{
private Question[] quiz;
private int count;
private final int MAX_QUESTIONS = 25;
public void Quiz ()
{
quiz = new Question[MAX_QUESTIONS];
count = 0;
}
public void add (Question q)
{
if (count < MAX_QUESTIONS)
{
// Throws a NullPointerException
quiz[count] = q;
count++;
}
}
public void Quiz ()
should be
public Quiz()
in order to be considered a constructor and correctly initialize your object. Otherwise, it is a method which you haven't invoked.

Declared boolean on object, doesn't change his value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I was just figuring out how instance variables are working, but it lead me to something strange.
public class DrumKit {
boolean topHat = true;
boolean snare = true;
void playTopHat() {
System.out.println("ding ding da-ding");
}
void playSnare(){
System.out.println("bang bang ba-bang");
}
}
public class DrumKitTestDrive {
public static void main(String [] args) {
DrumKit d = new DrumKit();
d.playSnare();
d.snare = false;
d.playTopHat();
if (d.snare == true);{
d.playSnare();
}
}
}
How is it possible that it outputs:
"bang bang ba-bang
ding ding da-ding
bang bang ba-bang"
And not: "bang bang ba-bang
ding ding da-ding"
Because what I thought was that the snare only would play once because I'm declaring it after d.playSnare();
to d.snare = false;
Remove the semicolon in this line, which is acting as the body for your if block.
if (d.snare == true);{
Change it to
if (d.snare == true){
Also, d.snare is already a boolean, so you can simplify the conditional expression to:
if (d.snare){
Here's the bang
if (d.snare == true);
Remove ;

Categories