Results 1 to 5 of 5
  1. #1
    Mod3rn is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Calling private int from another java file

    Okay, i know this is probably a simple question. But i would like to call a private int from another java file.
    For example, say i have a file named One.java and it has
    Java Code:
    private int shopId = 0;
    .
    Is there anyway i get call this int in a different file without making it public or static?

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

    Default Re: Calling private int from another java file

    That's exactly what the word 'private' means: nothing outside of that class can touch those privvy parts. You can define a public method though:

    Java Code:
    public int getShopId() { return shopId; }
    That method is called a 'getter' or 'accessor', so other objects of other classes can get the value of your private variable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Mod3rn is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Calling private int from another java file

    Where exactly do i put this? Under private int shopId = 0; or in the file requesting it?

  4. #4
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default Re: Calling private int from another java file

    u have to put it in class where your variable is.
    Java Code:
    public class Something {
    
    	private int shopId;
    
    	public int getshopId(){
    		return shopId;
    	}
    
    }

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

    Default Re: Calling private int from another java file

    Quote Originally Posted by Mod3rn View Post
    Where exactly do i put this? Under private int shopId = 0; or in the file requesting it?
    Think a bit: suppose that method were a method in the class requesting that int. The int is private so a method in another class can't refer to it (hennce your question), so it can't be a method in another class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. calling a .exe file from java code
    By ingudam in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:41 PM
  2. calling python file from java
    By cool in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2010, 03:50 AM
  3. Problem with calling php script from java file
    By eldushyant in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2010, 12:56 PM
  4. calling a jar file from within java application
    By robby14 in forum Advanced Java
    Replies: 3
    Last Post: 03-10-2010, 08:11 PM
  5. excecuting a jar file by calling a java class
    By Lavanya.vitria in forum Advanced Java
    Replies: 1
    Last Post: 12-13-2008, 12:11 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
  •