-
classes
Hi...
I'm sort of new to java programming! I wanted to know if you wanted to access a method in a different class what do you do.
I init the class in one class and then call it's members like so.
say you class loader and you want to use it's member's in class main, but they are in different .java files! how do you call that member function of loader!
loader lod = new loader();
lod.print(); ????
-
What you have there will work fine as long as long as the classes are in the same package. If they are not in the same package, you have to import the Loader class into the Main class.
If you don't know what a package is then just ignore it for now, and make sure the .class files are in the same folder.
-
Methods that are declared as public can be accessed all over the place.... Just make sure you import the package that contains the classes you want to access (or it won't compile)
eschew obfuscation
-
or if the methods are static, like the Math class, then you can call the method like this:
Math.sin()
-
Originally posted by Phaelax
or if the methods are static, like the Math class, then you can call the method like this:
Math.sin()
static methods/variables are implicitly public, there is no such thing as a private/protected static, you may declare it as private/protected, but you can access it like a public nevertheless....
eschew obfuscation
-
Static methods are not implicitly public. If you try to compile a class that is attempting to access a private static method from a different class, you will get a compile error. I just tried it.
Anyway, I think the point Phaelex was making is that you don't need to create an instance of the class to access it's static methods like you would if the method was non-static.
-
Of course...that was total nonsense, I don't knowwhere my mind was there
What I ment was the final static 'variables'
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks