Re: Some Beginner questions
Quote:
shadowing the class's field?
An example
Code :
class SomeClass {
int someVar = 33; // class variable
void someMethod() {
int someVar = 0; // a variable that shadows the class variable
...
int x = someVar; // gets the value of the local variable
int y = this.someVar; ; // gets the value of the class variable
...
} // end someMethod()
} // end class
Re: Some Beginner questions
Quote:
Originally Posted by
Norm
An example
Code :
class SomeClass {
int someVar = 33; // class variable
void someMethod() {
int someVar = 0; // a variable that shadows the class variable
...
int x = someVar; // gets the value of the local variable
int y = this.someVar; ; // gets the value of the class variable
...
} // end someMethod()
} // end class
Thanks for the reply
1. So, when you type this.someVar in a method, you are referring to the class's variable? Is it safe to say so in every case?
2.If I have the following code, would someMethod print out 33? Since I have not "shadowed" the someVar class variable?
Code java:
class SomeClass {
int someVar = 33; // class variable
void someMethod() {
system.out.println("someVar=" + someVar);
...
} // end someMethod()
} // end class
3. Say I have this
Code java:
class SomeClass {
int someVar = 33; // class variable
void someMethod() {
int x = this.someVar;
...
} // end someMethod()
} // end class
Why not just change int x = this.someVar into int x = 33?
Re: Some Beginner questions
1) this refers to the current instance of the class
2) Try it and see what is printed.
Quote:
Why not just change
That was a simple example not meant to have any realistic code.
Re: Some Beginner questions
The difference between imports and subclasing is this:
Suppose I have an Enemy class. And I have a class calles Atker that is a subclass of Enemy. You would probably, for neatness, put Atker and Enemy in the same package, called 'enemies'. If Atker was a subclass of Enemy, and Enemy contained a method called move(), then In the Atker class, we can use move(), as if it was in the class Atker.
Now suppose we have a class called World (Using Game examples because I find them easiest), World accesses methods from Entity like this:
Create object of Entity.
Call some methods from it. So only use a subclassing if it is inheriting a class, or is a specific thing. Atker is an Enemy, so we make it subclass of Enemy, but World isn't an enemy, so we make an object.
So the next time you see import pack.watevar, Remember that it only lets you create objects of that type, (Which in this case is watevar) because if it is in another package, your class can't see it!
Hope this helps.
Re: Some Beginner questions
Thanks! Just one final question. What if i wanted to call up a void method? Do i use subclassing?
Re: Some Beginner questions
Calling a method and extending a class (subclassing) are two completely different things.
Re: Some Beginner questions
Quote:
Originally Posted by
Norm
Calling a method and extending a class (subclassing) are two completely different things.
Now I'm really confused.
Extend a class = modify functionality right?
Import a class = ability to create objects without doing the classname.methodname right?
I just learnt of public, private and static modifiers
If i set a method to private, will extending the class in which the method belongs to give me access to use or overwrite it?
If i set a method to public, I can use it in any case, but cant overwrite it unless i extend the class it is in, right? O.O
Re: Some Beginner questions
I guess I assumed that you were asking how to call a method.
Now I see you are trying to define a method.
See the tutorial for all the details:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: Some Beginner questions
Is there any chat room for this forum around? I think I'm going to ask lots of questions haha =D.
Could anyone link me a somewhat advanced program I could learn from? Like, a command line calculator with user input, etc.
Edit:
I meant the sourcecode, apologies.
Re: Some Beginner questions
There is lots of source code on the forum in various threads.