Hey | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hey

Can non primitive variables be a instance variable?? in java

24th Jan 2020, 4:12 AM
Wasiq Abdullah
7 Answers
+ 5
Yes. I'm pretty sure it's an example of composition. https://code.sololearn.com/c6qNZbsNJ7xA/?ref=app
24th Jan 2020, 4:23 AM
Fermi
Fermi - avatar
+ 4
Hi Programmer</> Just a tip! Please keep your title relative to your problem. It will help users to understand what's the question without opening it. 😊
24th Jan 2020, 8:19 AM
Sachin Artani
Sachin Artani - avatar
+ 3
Programmer</> If you want to do so, make the nonPrimitive object static. Without the static keyword, test is an instance variable (which belongs to the instance of the class and not the class itself) and cannot be invoked in a static method. public class Program { private static nonPrimitive test = new nonPrimitive(); public static void main(String[] args) { System.out.print(test.value); } }
24th Jan 2020, 6:01 AM
Fermi
Fermi - avatar
+ 2
C++ python3 php I don't see relation to your tags
24th Jan 2020, 6:16 AM
Oma Falk
Oma Falk - avatar
+ 1
Sachin Artani Right bro👍
24th Jan 2020, 8:41 AM
Wasiq Abdullah
0
Fermi its means that we can create multiple objects of this nonprimitve variable
24th Jan 2020, 4:27 AM
Wasiq Abdullah
0
Fermi and why did u put (new nonPrimitive () ) inside Program () constructor.....??? and can we put new nonPrimitive () outside the constructor of Program () and inside Program class??
24th Jan 2020, 5:00 AM
Wasiq Abdullah