Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor questions  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
what i understand(maybe i dont understand it well) is that a word this inside constructor must be always(it is better right).There is wrong to use the word this inside of constructor or not?

second what does constructor?
what have i understand is that i use the name exactly of my class .If the name is vehicle of my class the name of the constructor will be the same name(vehicle).

third question
default constructor ?
why should i have default constructor?what it does actualy?

four question
i create objects ok . kop1 kop=new kop1(); as an example .What exactly does the objects?What are the difference between objects and that private String Fullname; ? the private String Fullname is object right?
i create methods with get and set and i use it .Which are the difference
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It is perfectly fine to use this in a constructor. In fact, for certain things, you have to.

2) Yes.

3) A default constructor is what you get when there are no explicit constructors. You don't have a choice whether you get one or not.

4) I have no idea what you are asking.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:. . . default constructor ?
why should i have default constructor?what it does actualy? . . .
You should avoid default constructors as far as possible, which you do by writing your own constructor in every class you write. A default constructor is only added if you write no constructors yourself. A default constructor does nothing; it simply allows you to create an object without initialising any fields. If your superclass does not have a no‑arguments constructor it may be impossible to create a default constructor.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:1) It is perfectly fine to use this in a constructor. In fact, for certain things, you have to.

2) Yes.

3) A default constructor is what you get when there are no explicit constructors. You don't have a choice whether you get one or not.

4) I have no idea what you are asking.
lets say i create an object kop1 kop=new kop1(); this .
if i create on the same class a private String name;


Anyway what i am asking is if i am in another class or if i am in main class .If i want to use the methods i should write only this setname(); getname(); ? if i want to create and an object for a name i should do that
n name=new n(); //i should use the n ? or not?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:. . . lets say i create an object kop1 kop=new kop1(); this .
if i create on the same class a private String name;
. . .
n name=new n(); //i should use the n ? or not?
Please use CapitalLetters for your class names; you can get some serious confusion otherwise.
Have you tried those bits of code? How many of them actually compile? How many of them will actually print a sensible value?Try some examples and report back with the results. I know the results, but you need to learn the mechanics of the program; it will not help you if I simply tell you.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . Try some examples and report back with the results. I know the results, but you need to learn the mechanics of the program; it will not help you if I simply tell you.
I will test it and I will write it another simple question : ++x or x++ inside methods or not are different?
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will test I will inform you ++x or x++ its the same right?(if it is or it isn't inside the method it is the same right?)
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this tutorial and later move on this list of tutorials. There you'll find many suggested ways how to resolve exercises faster. You have failed to implement first suggested material in all your 32 posted exercises.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:I will test I will inform you ++x or x++ its the same right?(if it is or it isn't inside the method it is the same right?)
The Java┬« Language Specification (=JLS) should tell you that i++ and ++i are different. The JLS can be difficult to read; look for the places where it says, “the value of the … expression …”.

Please don't quote the whole of a preceding post; that simply makes the thread longer. I have deleted such an unnecessary quote, and a post where you repeated yourself.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!