+ 1
Constructors in java
Showing a user message twice
15 Answers
0
this is wrong
ScreenShot() {
System.out.println("ScreenShot");
}
use name =
+ 3
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
String name = read.next();
//screenshots
ScreenShot screenshot1 = new ScreenShot();
ScreenShot screenshot2 = new ScreenShot(name);
//outputting the names
System.out.println(screenshot1.getName());
System.out.println(screenshot2.getName());
}
}
class ScreenShot{
private static String name;
// private static String screenshot;
//complete the first constructor
ScreenShot(){
System .out.println ("ScreenShot");
}
//complete the second constructor
ScreenShot(String name){
this.setName (name);
}
//Setter
public void setName(String name){
this.name = name;
}
//Getter
public String getName(){
return name;
}
}
+ 2
in first constructor you have to assign default name to name field, but you print it instead
+ 2
I think this is what happens
1. You create an object with your empty constructor. Your empty constructor PRINTS the string "ScreenShot", but doesn't save a value for name, so here name=null
2. You create an object with the other constructor. This one saves the name so name=name
3. You try to print both names of your created objects, so for the first object it PRINTS null then PRINTS whatever value name has recieved before
So if you don't want to save a name for an object (as your empty constructor does) don't try to print that name afterwards !
+ 1
because name is declared as static
private static String name;
so if you have two objects they have shared same name value
+ 1
in screenshot1
ScreenShot screenshot1 = new ScreenShot();
is used constructor
ScreenShot() {
System.out.println("ScreenShot");
}
where name is not initialised and is null
it prints first "ScreenShot" instead
then
System.out.println( screenshot1.getName()); //null
System.out.println( screenshot2.getName()); //name value
+ 1
what is problem in second constructor ?
+ 1
I can't see your actual code but you have two output commands in main()
Why you expect one output ?
+ 1
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.
//Java Program to create and call a default constructor
class Bus1{
//creating a default constructor
Bus1(){System.out.println("Bus is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bus1 b=new Bus1();
}
}
0
Even so , it still printout Screenshot then null then username
0
It is the second constructor whom i stuck in
0
It prints user input 2 times
I only need to print it once
0
At first if the user does not put a name to his screeshot it will be name ScreenShot by default else it will print ScreeShot and the user input
0
I didn't get it
0
Use static block inside the class to set the static fields
static {
//Set the name
name = "Name";
}