0

I understand how to use and import outside packages, but I've never packaged my own classes before. I read the Oracle Tutorial on Creating a Package, and looked at In Java, what's the difference between public, default, protected and private in addition to several sites/SO threads on packages. For the life of me, I can't figure out why this extraordinary simple example doesn't work:

package PTest;

public class A
{
    protected final int SIZE = 10; 

    public void printSize()
    {
        System.out.println(SIZE);
    }
}

package PTest;

public class B
{
    public static void main(String[] args)
    {
        System.out.println(SIZE);
        hello();
    }
}

I used eclipse's autopackaging feature, so I assume that the actual packing is correct. Here's an image to show that they are indeed packaged correctly: enter image description here

As you can see, neither the protected SIZE or the public hello() are recognized. I've tried this outside of eclipse, also to no avail. Any help would be greatly appreciated.

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • I got so caught up in the idea that it was something wrong with the way that I was using `package` that I completely forgot about main being `static`. This would've been abundantly clear had I gotten an error along the lines of: "can't access SIZE from a static context." Nonetheless, this is why I shouldn't program when I'm too tired. Sigh. Thanks everyone. – Steve P. Aug 07 '13 at 04:41

4 Answers4

2

SIZE is an instance field of A objects. You need to make it a static field. Even then, it'll be a member of the A class, so you have to specify A.SIZE to use it in B.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • That makes perfect sense. I didn't even think about main being `static`. I feel so stupid, thank you. – Steve P. Aug 07 '13 at 04:33
  • This would've been abundantly clear had I gotten an error along the lines of: "can't access SIZE from a static context." Do you know why this isn't the error? – Steve P. Aug 07 '13 at 04:42
  • @SanjayaLiyanage He says "you need to make it a static field." – Steve P. Aug 07 '13 at 04:46
  • @SteveP.: It's because you used `SIZE` instead of `A.SIZE`. The IDE doesn't know you want the field from `A`. If you'd specified `A.SIZE`, you would've gotten an error about accessing a non-static field from a static method. – user2357112 Aug 07 '13 at 04:53
  • @user2357112 That makes sense, I just though by virtue of putting them in the same package that it would know. Nonetheless, thanks for clearing that up. – Steve P. Aug 07 '13 at 04:54
2

Class methods cannot access instance variables or instance methods directly—they must use an object reference.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    Thanks I understand. I just completely forgot that main was `static`. It's been a long night. I guess what threw me off was that I didn't get an error along the lines of "can't access xyz from a static context".. – Steve P. Aug 07 '13 at 04:38
1

Errors you getting are fixed here

package PTest;

public class B
{
 public static void main(String[] args)
 {  
    A MyClassA = new A();               
    System.out.println(MyClassA.SIZE);
    MyClassA.printSize();
 }
}
Augustas
  • 1,167
  • 21
  • 31
  • Thanks I understand java, just completely forgot that main was `static`. For reference, you shouldn't use an identifier that has the same name as your class. – Steve P. Aug 07 '13 at 04:44
  • ty just came back to fix it and saw your answer ;) – Augustas Aug 07 '13 at 04:46
0

You can not directly access methods or fields which are not static (instance members) being in a static scope(main) other than using an object and then access or making those instance members as staic.(class members)

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
  • 1
    I think you're being downvoted because you're saying that it's not possible. Perhaps you should add that they can be found in a `static` context by invoking the classes' name and then using `.`. Nonetheless, I understood what you meant +1. – Steve P. Aug 07 '13 at 04:34
  • thanks Steve for understanding it correctly. I'll be more descriptive in next time – Sanjaya Liyanage Aug 07 '13 at 04:39
  • Might as well just edit your question, and perhaps the down-voter will remove their downvote. – Steve P. Aug 07 '13 at 04:40