classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: classes

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    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(); ????

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    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.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    or if the methods are static, like the Math class, then you can call the method like this:
    Math.sin()

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    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.

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center