+ 1

Constructors in java

Showing a user message twice

21st Oct 2021, 6:45 AM
Mohammed Amine
Mohammed Amine - avatar
15 Respostas
0
this is wrong ScreenShot() { System.out.println("ScreenShot"); } use name =
21st Oct 2021, 11:33 AM
zemiak
+ 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; } }
21st Oct 2021, 6:45 AM
Mohammed Amine
Mohammed Amine - avatar
+ 2
in first constructor you have to assign default name to name field, but you print it instead
21st Oct 2021, 11:29 AM
zemiak
+ 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 !
21st Oct 2021, 12:29 PM
Amaia Serra
+ 1
because name is declared as static private static String name; so if you have two objects they have shared same name value
21st Oct 2021, 8:55 AM
zemiak
+ 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
21st Oct 2021, 9:42 AM
zemiak
+ 1
what is problem in second constructor ?
21st Oct 2021, 11:18 AM
zemiak
+ 1
I can't see your actual code but you have two output commands in main() Why you expect one output ?
21st Oct 2021, 11:23 AM
zemiak
+ 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(); } }
21st Oct 2021, 2:49 PM
Arun Jamson
Arun Jamson - avatar
0
Even so , it still printout Screenshot then null then username
21st Oct 2021, 9:21 AM
Mohammed Amine
Mohammed Amine - avatar
0
It is the second constructor whom i stuck in
21st Oct 2021, 10:04 AM
Mohammed Amine
Mohammed Amine - avatar
0
It prints user input 2 times I only need to print it once
21st Oct 2021, 11:19 AM
Mohammed Amine
Mohammed Amine - avatar
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
21st Oct 2021, 11:26 AM
Mohammed Amine
Mohammed Amine - avatar
0
I didn't get it
21st Oct 2021, 11:30 AM
Mohammed Amine
Mohammed Amine - avatar
0
Use static block inside the class to set the static fields static { //Set the name name = "Name"; }
23rd Oct 2021, 2:56 AM
Abhishek Yadav
Abhishek Yadav - avatar