Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Some Beginner questions

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Some Beginner questions

    So I got really bored and decided to pick up java in my spare time over the past two weeks.
    The java tutorials have been extremely helpful in helping me understand the basics of classes, methods, etc., but when it really comes down to the more advanced things like field and method modifiers, I'm simply lost.
    Here's to you guys a couple of questions =D

    1. "The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter."
    Here's my problem. I don't get what's the difference between using the this operator and not using it. Dosen't the constructor's parameters belong already to the object that is being created? And what does it mean by shadowing the class's field?

    2. What is the difference between subclassing and import.filename? When you import another class to a class,the class gets all the methods of the imported class, similiar to subclassing, isn't it?

    I really hope to clear up these misconceptions as soon as possible D; I really feel that I shouldn't proceed any further without mastering the basics. Any help is greatly appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Some Beginner questions

    shadowing the class's field?
    An example
    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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Some Beginner questions

    Quote Originally Posted by Norm View Post
    An example
    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?
    class SomeClass {
       int someVar = 33;  // class variable
     
       void someMethod() {
         system.out.println("someVar=" + someVar);
         ...
       } // end someMethod()
    } // end class

    3. Say I have this
    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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Some Beginner questions

    1) this refers to the current instance of the class
    2) Try it and see what is printed.
    Why not just change
    That was a simple example not meant to have any realistic code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Some Beginner questions

    Thanks! Just one final question. What if i wanted to call up a void method? Do i use subclassing?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Some Beginner questions

    Calling a method and extending a class (subclassing) are two completely different things.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Some Beginner questions

    Quote Originally Posted by Norm View Post
    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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Some Beginner questions

    There is lots of source code on the forum in various threads.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  2. Few questions from beginner
    By Mefimess in forum Java Theory & Questions
    Replies: 2
    Last Post: November 6th, 2012, 11:03 AM
  3. Beginner: some questions
    By norenberg in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2012, 05:57 PM
  4. A few questions
    By elatechris777 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 06:33 PM
  5. A few questions
    By adenverd in forum Java Theory & Questions
    Replies: 3
    Last Post: May 26th, 2010, 03:34 AM