Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Calling a non-static method outside the package without using extends

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Calling a non-static method outside the package without using extends

    Hello everyone,

    I am currently refactoring my codes and as much as possible i don't want to use static methods to avoid any conflicts when it comes to class instantiation.

    Here's my problem,

    MyClass extends OtherClass{

    private myvar;

    public getMyvar{
    return myvar
    }
    }

    OtherClassOutsidePackage {

    //How to call getMyvar?? without declaring it as static

    }

    Your idea will be very helpful. Thanks!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Calling a non-static method outside the package without using extends

    Create a MyClass object and call the getMyvar() method.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a non-static method outside the package without using extends

    OtherClassOutsidePackage

    MyClass object = new MyClass();
    object.getMyVar()

    is this what you mean?

    this is not safe because the class is instantiated everytime it is called. I want only one instance created for my class to preserve the values of my instance variables.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Calling a non-static method outside the package without using extends

    Then it needs to be static.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a non-static method outside the package without using extends

    Ok, I know it needs to be static.

    Is there anyway where I can force the compiler to get the current instance of a class ??
    What I mean is, only MyClass can instantiate an object for MyClass. Other classes must go through MyClass before they can call getMyVar()?

    In this way, it will be fine to declare getMyVar() as static.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Calling a non-static method outside the package without using extends

    Sounds to me like you want MyClass to be a Singleton Object.

    That would look like:
    public class MyClass {
    	private static MyClass ref;
     
    	private myvar;
     
    	//Private constructor for restricted initialization
    	private MyClass() {
    		//do something, perhaps set myvar
    	}
     
    	//Method to get MyClass object
    	public static synchronized MyClass getMyClassObject() {
    		if(ref==null)
    			ref = new MyClass();
    		return ref;
    	}
     
    	//Secure against cloning
    	@Override
    	public Object clone() throws CloneNotSupportedException {
    		throw new CloneNotSupportedException();
    	}
     
    	//Your method
    	public getMyvar() {
    		return myvar;
    	}
    }

    Then, to get the myvar variable from anywhere in your program, simply call:
    //Get MyClass object
    MyClass myClass = MyClass.getMyClassObject();
    //Get variable
    myClass.getMyvar();

    It will always return the EXACT same MyClass object, regardless of how many times you call that from where ever in your program.
    Last edited by aussiemcgr; August 16th, 2012 at 02:15 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a non-static method outside the package without using extends

    wow..this is what i'm looking for!

    thanks!

Similar Threads

  1. is it possible to extends java class with another class from different package?
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 20th, 2012, 03:51 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM

Tags for this Thread