0

Why the constructor does not change class property? Or is there another reason?

class Practical{ int count; public void Practical(){ count = 7; } public static void main(String[] args) { Practical p = new Practical(); System.out.println(p.count); } }

27th Apr 2020, 12:26 AM
Aniket Singh Rana
Aniket Singh Rana - avatar
1 Réponse
+ 2
You declared the constuctor wrong. You declared it like a method. Remove “void” and it will work. public Practical() { count = 7 }
27th Apr 2020, 12:49 AM
Chris Persichetti
Chris Persichetti - avatar