• Post Reply Bookmark Topic Watch Topic
  • New Topic

static and final modifiers  RSS feed

 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm trying to understand if these classes would compile or not? Please, help me to find out



 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you tried to compile them, whats happened?
 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks When I try to test them it looks like them works correctly, but I'm still not sure. In the second case I don't understand why the static method accesses the non-static variable int x = 12.


 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these code snippets for a preparation for one of Java certifications?
 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they are from the book "Head First Java"
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tatiana Petrova wrote:In the second case I don't understand why the static method accesses the non-static variable int x = 12.

Below is your 2nd code example

1. On line 3 you're creating an instance of class Foo6 and assigning it to a reference variable a.
2. Class Foo6 has one instance variable x, which has default (also called package private) access modifier. That means variable x can be accessed directly by any class defined in the same package.
3. I pressume your classes Ideone and Foo6 are in the same package.
4. Class Foo6 has static method go which has defined parameter x.
5. So, on line 4 you just simply invoking static method go directly via class name in which he is defined, and supplying non private variable x, which can be accessed directly via reference variable a (in case class Ideone is defined in the same package as class Foo6, in your case I think it is, otherwise you wouldn't have such question).

What you cannot do is:
 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they're in the same package, of course. Thanks a lot, it's more clear now. So, can we say that both of the classes - Foo5 and Foo6 are ok and there aren't any problems with them?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tatiana Petrova wrote:can we say that both of the classes - Foo5 and Foo6 are ok and there aren't any problems with them?
That depends on your goal.

  • Class should compile
  • It should produce expected output on all inputs (having said that, it should be tested to prove that)
  • It should follow best practices, that is vogue, some of them are:
  • 1. Should follow Java coding conventions
    2. Methods shouldn't violate single responsibility principle
    3. Classes should be well encapsulated
    4. Class should be documented (JavaDoc's)
    ...

    Currently your class/-es violates rules 1, 3 and 4. If you look from this perspective - then classes are not ok and there are some problems.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tatiana Petrova wrote:When I try to test them it looks like them works correctly, but I'm still not sure. In the second case I don't understand why the static method accesses the non-static variable int x = 12.

    I think Liutauras has explained it to you, but I have a few general tips for you:

    1. Avoid static variables (that is, static fields without a final qualifier) completely.

    2. Avoid static methods (other than main()) in general. They do have their place, but while you're learning the language, try to create objects and write methods for them.

    Specifically: DON'T make a method or variable static simply because you're having access problems, and adding static gets rid of your compiler errors.
    Instead, try and work out how to get your classes to work without the static keyword. It may be a little trickier right now, but you'll thank me in a few month's time.

    And it goes back to Liutauras's last post: Just because a class compiles, doesn't necessarily mean that it is:
    (a) good.
    (b) going to work.
    Java is an Object-Oriented language, so you're far better off creating objects to do your work, even if it isn't immediately obvious why right now.

    This page may help you to avoid falling into the "static trap".

    HIH

    Winston
     
    Fred Kleinschmidt
    Bartender
    Posts: 572
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tatiana Petrova wrote:
    When I try to test them it looks like them works correctly, but I'm still not sure. In the second case I don't understand why the static method accesses the non-static variable int x = 12.


    And here's the code:


    The real answer is that the static method is NOT accessing the non-static variable x=12. Your main method is equivalent of:



    So your main method is accessing the non-static instance variable of a Foo6 instance "a".
    Then you are passing that value (y) to the static method Foo6.go(). This is exactly the same as calling



     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred, have a cow for a nice step in and another way to visualize this. I think your way is better than mine is
     
    Fred Kleinschmidt
    Bartender
    Posts: 572
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for the cow - barbecuing tonight, I'll provide the wine...
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!