Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree2Likes

Thread: Importing classes

  1. #1
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Importing classes

    I have a program that uses 2 classes: 'main.class' and 'map/read.class'.
    These are the (essential parts of the) files:

    map/read.java
    Java Code:
    package map;
     
    public class read {
        public static void main(String[] args) {
            String foo = "bar";
     
            // Doing some stuff
        }
    }
    main.java
    Java Code:
    import map.read;
    import java.io.*;
     
    public class main {
        public static void main(String[] args) {
            System.out.println(foo);
    
            // Doing more stuff
        }
    }
    When I run main.class, I want the console to display "bar", but instead it gives me the next error:

    main.java:6: cannot find symbol
    symbol : variable foo
    location: class main
    System.out.println(foo);
    ^
    1 error

    (Note: the '^' is being displayed underneath the 'f' of 'foo')
    Anyone has any idea why?

  2. #2
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Importing classes

    Because in Class main there is no foo. The static void main method is your entry/exit point so it makes no sense to have it in both classes. If you say java main it will run the second one and if you say java read it will run the first one (assuming your classpath is setup to run them or that you are in the right dir).

    To get the main class to see the read class variable it has to be a static class variable or part of an object. To do the former move it to just below the class statement and add the keyword static. Then it will always be available and you can access it as read.foo (as long as it has public access). The other way would be to rename the main method in read to something else, say myMethod, and instantiate it in main with "myMethod mine = new myMethod();" Then you will have a reference to a read object and you can access its public variables, which read.foo is not, with mine.foo.
    Fubarable likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Importing classes

    You need to get used to the naming standards.
    Classes start with a capital (so Read and Main).
    It makes it easier to see which things you are talking about are methods or classes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    @gillbates
    Thank you for your help: I've managed to rewrite my code, but not without errors.

    main.java
    Java Code:
    import java.io.*;
    
    
    public class main {
        public static void read() {
            String foo = "bar";
        }
    
        public static void main(String[] args) {
            System.out.println(read.foo);
        }
    }
    The error I get:

    ../main.java:10: cannot find symbol
    symbol : variable read
    location: class main
    System.out.println(read.foo);
    ^
    1 error

    (Note: the '^' is underneath the 'r' of 'read.foo')
    Last edited by Waflix; 07-11-2012 at 01:07 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,521
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing classes

    I don't see an import statement for that 'read' class and neither does the compiler ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Importing classes

    Or are you trying to call the method read()?
    See my previous post about naming conventions, as this is a prime example of the confusion that can be caused by not following them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    @Tolls
    I've already updated the actual file with these standards, but forgot to do so with the simplified one I just posted. And I take the compiler doesn't follow these naming conventions, so it shouldn't cause a problem in this particular script, right?

    @JosAH
    And how am I to import the read method? "read read = new read();" doesn't seem to work for me:

    ../main.java:10: cannot find symbol
    symbol : class read
    location: class main
    read read = new read();
    ^ (@ first read)

    ../main.java:10: cannot find symbol
    symbol : class read
    location: class main
    read read = new read();
    ^ (@ third read)
    2 errors

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Importing classes

    No the compiler doesn't care, but if you expect people (ie us) to be able to easily see what you're trying to do it helps to use the naming standards.

    Jos thinks you are trying to use the 'read' class, because 'read.foo' implies the class (or a variable).
    If you used the proper naming standards we wouldn't be having this confusion as it would be clear what you are trying to do.

    If you are trying to call the method then just call read().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    Then I have 2 questions left.

    1) How do I call method read?
    2) And after that, how do I access the variable foo?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,521
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing classes

    Read about static imports; i.e. you can import all static members from a class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    I've decided to re-divide the program into two files: map/Read.java and Main.java.

    map/Read.java remains the same
    Java Code:
    package map;
    
    public class Read {
        public static void read() {
            String foo = "bar";
        }    
    }
    Main.java
    Java Code:
    import map.Read;
    import java.io.*;
    
    
    public class Test {
        public static void main(String[] args) {
            Read something = new Read();
            System.out.println(/* ? */);
        }
    }
    Now, do I replace String foo with the question mark?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,521
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing classes

    I can see only one foo and that's a variable local to a main( ... ) method in chass Read; it cannot be used, nor imported, anywhere outside of that method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    I can't seem to find a main() method in class Read.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,521
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing classes

    Quote Originally Posted by Waflix View Post
    I can't seem to find a main() method in class Read.
    Sorry for that: I mean the read() method ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default Re: Importing classes

    I thought "public ... read()" would make anything in it public, but according to you it doesn't. Am I supposed to put 'public' in front of 'String foo = "bar";' as well, then?
    ~ Waflix

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,521
    Blog Entries
    7
    Rep Power
    20

    Default Re: Importing classes

    Have you read any tutorials on the subject? They explain it well and you don't need to guess (as you do now); public access means that anything can access the public something. Local variables are always local to a method and only exist when they are in scope when the method runs. You can't make them public.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Importing classes

    The Java tutorials.
    Start with the basics.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Importing classes

    Static version: (2 files as you can only have 1 public class per file).
    Java Code:
    public class Read {
         public static String foo = "bar";
         public void read() {
        }    
    }
    
    Main.java
    
    import map.Read;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(Read.foo);
        }
    }
    Instantiated version: (2 files).
    Java Code:
    package map;
     
    public class Read() {
        public String foo;
        
        public void ReadFoo() {
            foo = "bar";
        }
    
        public String ReadFoo2() {
           String foo2 = "bar2";
           return foo2;
        }
    }
    main.java
    
    import map.read;
     
    public class main {
        public static void main(String[] args) {
            Read myFoo = new Read(); // instantiate the Read object
            myFoo.ReadFoo();  // initialize the instance variable foo
            System.out.println(myFoo.foo);
            System.out.println(myFoo.ReadFoo2());
        }
    }
    What's going on:

    In the static version I created a static variable called foo and initialize it to "bar". The Read function doesn't do anything. foo is considered a class variable (like a global) and does not require you to instantiate (create) an object for it. In fact if you create an object of the static version of Read it will not include the foo variable. Since it's a class variable you just refer to it with the class name (Read.foo). If you have instantiated the Read object (Read myWhatEver = new Read();) you can refer to it as myWhatEver.foo but the compiler really just gets Read.foo and nags at you about your lack of understanding of how to access static variables.

    In the non-static version foo is an instance variable. It doesn't exist outside of an object. If you ask for Read.foo, before creating the object, he will tell you that there is no Read.foo. So you have to instantiate Read to create foo. Everytime you instantiate foo you will get a new Read object and every Read object will have a foo. The readFoo method sets the foo instance variable for its object to "bar". If you try to print foo before calling readFoo() he will print null. I could have set foo to 'bar' when I initialized it but then I didn't so you have to call readFoo() first. Since foo is an instance variable it can be accessed by any method in the class. Since it's public it's also available to everyone else which is considered poor form (see getters/setters, encapsulation).

    In the second method, readFoo2, I create a local variable. Unlike instance variables, local variables are not available outside of the block they are declared in. I specified that readFoo2 returns a String and returned the String in my local variable directly to the println method in the calling program.

    Generally you should prefer code that uses local and instance variables over class variables.
    Waflix likes this.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Importing classes

    Please don't spoonfeed code to people, with or without explanation.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Importing classes

    Really? I'm not trying to be difficult but I just gave him code that will print:

    bar
    bar2

    I also explained class, instance, and local variables. I also touched on encapsulation and access control. My intent was to help him understand basic declarations so he could move on to more interesting things. Now if I misstated something feel free to correct me. In fact I hope you will.

    I agree that you shouldn't spoon feed answers to people, but he had already tried to follow the more general instructions, from my first post, and was still struggling. I figured he had suffered enough and was ready to be told what was going on.
    Last edited by gillbates; 07-11-2012 at 07:11 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Importing packages and classes
    By tnrh1 in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 02:35 PM
  2. importing classes
    By javajames in forum New To Java
    Replies: 6
    Last Post: 05-24-2011, 09:21 AM
  3. importing other classes or using a Jar
    By xhane in forum New To Java
    Replies: 3
    Last Post: 01-31-2009, 05:20 AM
  4. Need help with importing classes
    By Deathmonger in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:03 AM
  5. Importing classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •