Results 1 to 20 of 23
Thread: Importing classes
- 07-10-2012, 10:25 PM #1
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
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
main.javaJava Code:package map; public class read { public static void main(String[] args) { String foo = "bar"; // Doing some stuff } }
When I run main.class, I want the console to display "bar", but instead it gives me the next error:Java Code:import map.read; import java.io.*; public class main { public static void main(String[] args) { System.out.println(foo); // Doing more stuff } }
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?
- 07-10-2012, 11:21 PM #2
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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.
- 07-11-2012, 10:10 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 07-11-2012, 12:36 PM #4
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
Re: Importing classes
@gillbates
Thank you for your help: I've managed to rewrite my code, but not without errors.
main.java
The error I get: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); } }
../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.
- 07-11-2012, 12:44 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Importing classes
I don't see an import statement for that 'read' class and neither does the compiler ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2012, 12:53 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 07-11-2012, 01:06 PM #7
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
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
- 07-11-2012, 01:30 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 07-11-2012, 01:48 PM #9
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
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?
- 07-11-2012, 01:59 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Importing classes
Read about static imports; i.e. you can import all static members from a class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2012, 02:06 PM #11
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
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
Main.javaJava Code:package map; public class Read { public static void read() { String foo = "bar"; } }
Now, do I replace String foo with the question mark?Java Code:import map.Read; import java.io.*; public class Test { public static void main(String[] args) { Read something = new Read(); System.out.println(/* ? */); } }
- 07-11-2012, 02:26 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2012, 02:30 PM #13
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
Re: Importing classes
I can't seem to find a main() method in class Read.
- 07-11-2012, 02:34 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 07-11-2012, 02:36 PM #15
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
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
- 07-11-2012, 02:49 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2012, 03:56 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Importing classes
The Java tutorials.
Start with the basics.Please do not ask for code as refusal often offends.
- 07-11-2012, 05:54 PM #18
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
Re: Importing classes
Static version: (2 files as you can only have 1 public class per file).
Instantiated version: (2 files).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); } }
What's going on: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()); } }
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.
- 07-11-2012, 06:16 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Importing classes
Please don't spoonfeed code to people, with or without explanation.
Please do not ask for code as refusal often offends.
- 07-11-2012, 06:51 PM #20
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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.
Similar Threads
-
Importing packages and classes
By tnrh1 in forum New To JavaReplies: 3Last Post: 11-07-2011, 02:35 PM -
importing classes
By javajames in forum New To JavaReplies: 6Last Post: 05-24-2011, 09:21 AM -
importing other classes or using a Jar
By xhane in forum New To JavaReplies: 3Last Post: 01-31-2009, 05:20 AM -
Need help with importing classes
By Deathmonger in forum New To JavaReplies: 3Last Post: 02-07-2008, 10:03 AM -
Importing classes
By Java Tip in forum Java TipReplies: 0Last Post: 11-06-2007, 02:27 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks