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

Am I understanding this correctly?  RSS feed

 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Just want to make sure that I am understanding the following correctly?



If not, can you let me know what I am not understanding? Additionally, is there any specific reason / benefit to having E and F outside of the constructor? Also, 2 things regarding methods:
1) I read that methods are required to have a return type, but what if you set the method as void? I thought a return type is not needed in that case.
2) I also read that methods can have the same name as the class, but would this not actually be a bad idea and somewhat confusing?

Thanks guys,

Ben
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Poland wrote:...is there any specific reason / benefit to having E and F outside of the constructor? Also, 2 things regarding methods:
1) I read that methods are required to have a return type, but what if you set the method as void? I thought a return type is not needed in that case.
2) I also read that methods can have the same name as the class, but would this not actually be a bad idea and somewhat confusing?

Having 'e' and 'f' (not 'E' and 'F') outside of the constructor means that they will be available to all of the class's methods. These are referred to as 'fields'. Because your fields are 'private' (which they should be), they are not directly accessible outside of the class unless you provide getters and/or setters.

You have to declare a method's return type. If 'void' then no value is returned, otherwise a value will need to be returned.

Well, methods can't have the same name as the class. The method and class name should at least differ by character case - a class name should begin with an upper-case letter and a method should start with a lower-case letter.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Carey,

So in the example of e and f they would apply an initial state of an object? How would this differ from having them inside a constructor? Or is there effectively no difference? Private made sense to me as I would only want them available to this specific class. Is an individual constructor created for each object? I assume yes. If so, will the e and f fields be applied to each constructor? Anything else specified would be object specific? Also, if e and f are applied to every object and that's what you'd want would this be the main reason to specify them outside of a constructor?

I hope I am making sense... lol

Thanks,

Ben
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you've got it, e and f are declared outside of the constructor. This is necessary in order to share them between all the (non-static) methods. You are also initializing e and f to specific values, this could be done as you have done at time of declaration, or the initialization could take place inside the constructor. The way you have it is more typical because the initialization is shared among all constructors that you provide for your class. On the other hand, if e and f are dependent on parameters passed in to the constructor then you'd have to hold off initializing them till you get inside the constructor.

When you create a new object instance of your class, storage will be allocated in each instance for their own values, e and f storage is not shared among different instances.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it...

Ok, so a couple of changes:



Ok, so I just changed e and f about a bit and have constructor overloading... In this case, e and f are only declared fields, but initialised in the constructor and have to be static or they won't be available to the constructors? Also, I can't leave these as final now as they have not been initialised? In the previous example, had I tried to change the value of e and f in the constructor, but left them as final that change would not be applied? And as e and f are now initialised in the constructors, is memory ONLY allocated to them at that point? In this case I have set f to have a different value in each constructor. Would I also be right to say that constructor overloading is only possible if you have a different number of parameters set in each?

So many questions... Sorry :/

Ben
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class is the creation of an object, the object is the implementation of the class

I would say, A class is the blueprint for an object; the object is an instance of the class.
This is the constructor. It cannot have any return type or non access modifiers

Actually, a constructor can be private. You might want to do this to insure the class can't be instantiated or because you want instantiation to be controlled in another way, like a factory.
A default constructor (no arguments) is created when "new" is used

Not quite. A default constructor is created when no constructor is explicitly written. The new keyword is used to create an object with any constructor.
I also read that methods can have the same name as the class, but would this not actually be a bad idea and somewhat confusing?

It would be a bad idea and it sometimes happen when beginning programmers try to put a return type on a constructor.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not work as intended

In this constructor you actually have a parameter named 'e' and a field named 'e'. The parameter 'e' obscures the field of the same name, and so you must use the 'this' keyword to get access to the field.

This is a typical usage. I tossed out the '45' and '55' because it is obvious you will be passing these in as parameters to the constructor.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Knute

I would say, A class is the blueprint for an object; the object is an instance of the class.


Ok, got the difference...

Actually, a constructor can be private. You might want to do this to insure the class can't be instantiated or because you want instantiation to be controlled in another way, like a factory.

Factory? Is setting a constructor to private uncommon? I only ask that because I haven't seen that mentioned anywhere I've read so far. Seems to me like it would be sensible practice though :/

Not quite. A default constructor is created when no constructor is explicitly written. The new keyword is used to create an object with any constructor.


Ok a constrcutor is created regardless of the "new" keyword? New just gives an object a constructor? Essentially the constructor is the object initialiser?


In the 2nd exmaple, Carey, "this" is saying use the e and f in the field or the constructor parameter? I read a little about "this" but was a little confused. Will play around with it.

Thanks guys

Ben
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Poland wrote:Got it...

Ok, so a couple of changes:



Ok, so I just changed e and f about a bit and have constructor overloading... In this case, e and f are only declared fields, but initialised in the constructor and have to be static or they won't be available to the constructors? Also, I can't leave these as final now as they have not been initialised? In the previous example, had I tried to change the value of e and f in the constructor, but left them as final that change would not be applied? And as e and f are now initialised in the constructors, is memory ONLY allocated to them at that point? In this case I have set f to have a different value in each constructor. Would I also be right to say that constructor overloading is only possible if you have a different number of parameters set in each?

So many questions... Sorry :/

Ben

I went back to look at this modified code more closely. You seem to be thrashing about and making changes to get the compiler to stop generating errors.



Fields should not be static. You probably did this because of error messages, but it is wrong.

If you have a constructor that takes arguments you must pass in arguments that have some useful value. Here you have no value for e and f once you get rid of the static modifier for your fields.

This constructor call actually passes in some values.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, very much so... About the thrashing about, get rid of errors thingy.

I just went back to double check the error that was being generated "Non static variable e cannot be referenced from a static context", same applies to f of course. Is that error only getting thrown because the argument itself that's being passed didn't have values? I did as you have done here and the error disappeared. Would you not set a field as static as a change made to it in one place would also be applied everywhere else that instance exists? Static is relative to type right?

Thanks,

Ben
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your (non-static) fields have storage allocated for each instance that you create with the new operator. Static member variables have only one set of storage allocated regardless of how many instances you have. All static members are shared for all instances. In essence you've created a sort of 'global' variable inside the class.

You were getting an error because your main() method did not have its own e and f variables.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class's static methods do not have access to the non-static class fields. This applies to the main() method as well.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Understood Thank you!

I'll play around with this a bit more. Just want to make sure that this all sinks in for now

No doubt I'll have plenty more questions later to harass with :/

Yeah yeah, I'm a pest, I know

Thanks again,

Ben
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would the only reason to set static fields be if you wanted them specifically to be accessed in methods? Is there any other reason? Or should it just be avoided?

Ben
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. I'm shutting down for the night.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to shut down most nights, just seem incapable of doing so at the moment! :O

Happy days

Thanks for the help again, Carey... G'night

Ben
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Poland wrote:
Actually, a constructor can be private. You might want to do this to insure the class can't be instantiated or because you want instantiation to be controlled in another way, like a factory.

Factory? Is setting a constructor to private uncommon? I only ask that because I haven't seen that mentioned anywhere I've read so far. Seems to me like it would be sensible practice though :/

Sorry, I should have said "Builder". I have an example of a builder pattern here in a program I wrote to "cheat" at Scrabble.

Ben Poland wrote:
Not quite. A default constructor is created when no constructor is explicitly written. The new keyword is used to create an object with any constructor.

Ok a constrcutor is created regardless of the "new" keyword? New just gives an object a constructor? Essentially the constructor is the object initialiser?

That's right, a constructor is an object initializer. The keyword new doesn't create a default constructor, the compiler does.

Ben Poland wrote:
In the 2nd exmaple, Carey, "this" is saying use the e and f in the field or the constructor parameter? I read a little about "this" but was a little confused. Will play around with it.

The keyword this is just a reference to the instance of the class. So if you have a local variable and an instance field both called e, this.e would point to the instance variable.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Poland wrote:So would the only reason to set static fields be if you wanted them specifically to be accessed in methods? Is there any other reason? Or should it just be avoided?

Beginning programmers should wary of static variables, because they often mean that you are writing static methods when you should be writing instance methods. Static methods have their place, such as in utility classes, but should generally be avoided.
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey again,

Wanted to make sure that got properly drilled into my head and now it is Plenty more questions to come soon I guess :/

Once again, thanks so much for the help guys

Ben
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Poland wrote:I just went back to double check the error that was being generated "Non static variable e cannot be referenced from a static context", same applies to f of course.

As Knute said earlier, you should try to avoid the static keyword as much as you possibly can while you're still starting out.
The trouble is that there is one method that MUST be static: main(), so if you intend to run the class, you do have to write at least one static method.

It's probably worth pointing out that most classes aren't run directly, and if you have an application with a hundred classes in it; only ONE of them needs a main() method.

However, while you're starting out, you'll probably be creating a lot of classes that you "write and run", so a simple pattern for avoiding static is as follows:And the code in main() should always look the same - Two lines: an object assignment, followed by a call to run().

For more information, have a look at Main Is A Pain.

HIH

Winston
 
Ben Poland
Ranch Hand
Posts: 97
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do that with everything I write that I actually intend on continuing I always call it classnameLauncher, then close it and don't touch it again after it's done. I didn't in the other thread because I wanted to write something quickly to give a more visual picture of what I had in my head. I have a tendency, or at least I think I do of rambling and maybe not making as much sense as I hope to... lol And yes, avoiding static usage for a while I think

Ben
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!