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:)

18th Jun 2019, 5:54 PM
Khai123
Khai123 - avatar
18 Respuestas
+ 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. ;)
19th Jun 2019, 1:47 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
No. At the moment it's just a hobby.
19th Jun 2019, 1:53 AM
Denise Roßberg
Denise Roßberg - avatar
+ 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
18th Jun 2019, 10:56 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
it starts to click.appreciate your help
19th Jun 2019, 1:02 AM
Khai123
Khai123 - avatar
+ 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;
19th Jun 2019, 1:17 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
got it bro. you have cleared my doubt. i will make some codes tomorrow to understand this concept deeply
19th Jun 2019, 1:26 AM
Khai123
Khai123 - avatar
+ 1
Your welcome :) Btw: It took me also a while to understand this topic.
19th Jun 2019, 1:32 AM
Denise Roßberg
Denise Roßberg - avatar
0
A code about static vs. non static methods: https://code.sololearn.com/cYLsh8s9OWlY/?ref=app
18th Jun 2019, 10:59 PM
Denise Roßberg
Denise Roßberg - avatar
0
how you add class Program ?, post example where is start { and end }
18th Jun 2019, 11:06 PM
zemiak
18th Jun 2019, 11:08 PM
Denise Roßberg
Denise Roßberg - avatar
0
zemiak What do you mean?
18th Jun 2019, 11:10 PM
Denise Roßberg
Denise Roßberg - avatar
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.
19th Jun 2019, 12:37 AM
Khai123
Khai123 - avatar
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
19th Jun 2019, 1:01 AM
Denise Roßberg
Denise Roßberg - avatar
0
How long have you learnt programming?
19th Jun 2019, 1:36 AM
Khai123
Khai123 - avatar
0
i have to learn java b/c my college focuses mainly on this lang
19th Jun 2019, 1:36 AM
Khai123
Khai123 - avatar
0
oh are you in college?
19th Jun 2019, 1:48 AM
Khai123
Khai123 - avatar
0
so you are still a high school student, right?
19th Jun 2019, 1:54 AM
Khai123
Khai123 - avatar
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.
19th Jun 2019, 5:45 PM
Khai123
Khai123 - avatar