0
same output & different sysntax
Hello guys, I just learnt Java for several days. These two codes I created made confused. The codes can swap two variables of 2 objects. In one code, if you add public class Program to it, it will generate some errors. However, in another one, the code just works if you do not type in class Program. As a beginner, I guest that because of the "static" class. This is links to my 2 diff codes:https://code.sololearn.com/cH2kINAHjkDs/#java. The second one:https://code.sololearn.com/clP9i4V38zWA/#java. After playing around 2 codes, I realized that if you put in static class you have to put in class Program. But if you dont put in static, you have to remove class Program. I kind of remember it but dont understand it:)
18 odpowiedzi
+ 3
I started with Java two years ago. But I did not know Sololearn yet, so I had little motivation to regularly learn and practice. I've been here for about a year now, since then I've been working on Java pretty much every day. Where there is still enough that I do not know. ;)
+ 2
No. At the moment it's just a hobby.
+ 1
In your first code Student is an outer class. Create an object, do some stuff with int age.
The second example is a bit more complicate.
At first you should know more about the keyword static.
In short: a static variable/method/class doesn't belong to the instance (object) of a class. Like your method swap. To work with swap you don't create an object.
Non static stuff belongs to an instance of a class. Like in your first code. Int age is non static. You need to create an object of Student.
In your second code class Student is an static inner class of your main class. You don't need to create an object of your main class.
Here is a code how it works to create an object of a non static inner class.
https://code.sololearn.com/c8JWfIbCE8JH/?ref=app
+ 1
it starts to click.appreciate your help
+ 1
You can also play around with your first code.
The class Student has a variable int age.
In your main class you need to create an object.
Student jenny = new Student();
jenny.age = 23;
-> age belongs to object jenny
Now add another variable to the class Student but use static: e.g. static int number;
number does not belong to jenny (or any object of Student) but it belongs to class Student.
So in your main class:
Student.number = 114;
+ 1
got it bro. you have cleared my doubt. i will make some codes tomorrow to understand this concept deeply
+ 1
Your welcome :)
Btw: It took me also a while to understand this topic.
0
A code about static vs. non static methods:
https://code.sololearn.com/cYLsh8s9OWlY/?ref=app
0
how you add class Program ?, post example where is start { and end }
0
About the keyword static:
https://www.baeldung.com/java-static
About inner classes:
https://www.tutorialspoint.com/java/java_innerclasses.htm
0
zemiak What do you mean?
0
i think that i have a better understading of this. In the code that gave me, i can call static method without creating object. But for the non static, it is related to class Program. we have to type in Program.Nonstatic. but for the static Staticexample s=new Staticexample() is enough. that is what i understand so far.:(( sr bro. I just deleted the object created for using non static and it did not work. However when i recreated it, it worked. I kind of understand. thanks.
0
Khai123
Program prog = new Program()
-> class NonStaticExample is part of the object prog
-> class StaticExample is part of the class Program but not part of the object prog
0
How long have you learnt programming?
0
i have to learn java b/c my college focuses mainly on this lang
0
oh are you in college?
0
so you are still a high school student, right?
0
Because int static number does not belong to any object of class Student like khai, jen. So we need to type in"Student. number". I realized that I could pass number to khai, or jen for example:"khai.number", jen.number". And it still worked.