How to access variables in another package in java?











up vote
-1
down vote

favorite












I am trying to access variables in another package in java, I tried some code:



The first package contains two classes, this is the first one



package encapsuation;
import s2.foo;

public class Encapsulation {

public static void main(String args) {
s1 a = new s1();
a.age = 21;
a.name = "ahmed";

a.print();
foo.print();
}

}


The second one, this is what I need to access in the class of the second package.



package encapsuation;

public class s1 {
public static String name;
public static int age;

public static void print()
{
System.out.println(name + " " + age);
}
}


The second package contain one class



package s2;
import encapsuation.s1;

public class foo{

public static void print()
{
System.out.println(s1.name + " " + s1.age);
}

}


Is what I am trying correct?



And why I cannot access the variables in class of the second package in the class "public class foo" but can access them in the method print only?










share|improve this question




















  • 1




    Any specific reason why you made name and age static? (and the print() method not static)
    – SirDarius
    Nov 10 at 19:10








  • 1




    It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
    – Joakim Danielson
    Nov 10 at 19:14










  • I have edit the code and make the method static
    – Hadeer Zayat
    Nov 10 at 19:26










  • I just need explanation for my question
    – Hadeer Zayat
    Nov 10 at 19:26










  • The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
    – John B. Lambe
    Nov 10 at 21:05

















up vote
-1
down vote

favorite












I am trying to access variables in another package in java, I tried some code:



The first package contains two classes, this is the first one



package encapsuation;
import s2.foo;

public class Encapsulation {

public static void main(String args) {
s1 a = new s1();
a.age = 21;
a.name = "ahmed";

a.print();
foo.print();
}

}


The second one, this is what I need to access in the class of the second package.



package encapsuation;

public class s1 {
public static String name;
public static int age;

public static void print()
{
System.out.println(name + " " + age);
}
}


The second package contain one class



package s2;
import encapsuation.s1;

public class foo{

public static void print()
{
System.out.println(s1.name + " " + s1.age);
}

}


Is what I am trying correct?



And why I cannot access the variables in class of the second package in the class "public class foo" but can access them in the method print only?










share|improve this question




















  • 1




    Any specific reason why you made name and age static? (and the print() method not static)
    – SirDarius
    Nov 10 at 19:10








  • 1




    It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
    – Joakim Danielson
    Nov 10 at 19:14










  • I have edit the code and make the method static
    – Hadeer Zayat
    Nov 10 at 19:26










  • I just need explanation for my question
    – Hadeer Zayat
    Nov 10 at 19:26










  • The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
    – John B. Lambe
    Nov 10 at 21:05















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to access variables in another package in java, I tried some code:



The first package contains two classes, this is the first one



package encapsuation;
import s2.foo;

public class Encapsulation {

public static void main(String args) {
s1 a = new s1();
a.age = 21;
a.name = "ahmed";

a.print();
foo.print();
}

}


The second one, this is what I need to access in the class of the second package.



package encapsuation;

public class s1 {
public static String name;
public static int age;

public static void print()
{
System.out.println(name + " " + age);
}
}


The second package contain one class



package s2;
import encapsuation.s1;

public class foo{

public static void print()
{
System.out.println(s1.name + " " + s1.age);
}

}


Is what I am trying correct?



And why I cannot access the variables in class of the second package in the class "public class foo" but can access them in the method print only?










share|improve this question















I am trying to access variables in another package in java, I tried some code:



The first package contains two classes, this is the first one



package encapsuation;
import s2.foo;

public class Encapsulation {

public static void main(String args) {
s1 a = new s1();
a.age = 21;
a.name = "ahmed";

a.print();
foo.print();
}

}


The second one, this is what I need to access in the class of the second package.



package encapsuation;

public class s1 {
public static String name;
public static int age;

public static void print()
{
System.out.println(name + " " + age);
}
}


The second package contain one class



package s2;
import encapsuation.s1;

public class foo{

public static void print()
{
System.out.println(s1.name + " " + s1.age);
}

}


Is what I am trying correct?



And why I cannot access the variables in class of the second package in the class "public class foo" but can access them in the method print only?







java oop encapsulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 0:57

























asked Nov 10 at 19:06









Hadeer Zayat

53




53








  • 1




    Any specific reason why you made name and age static? (and the print() method not static)
    – SirDarius
    Nov 10 at 19:10








  • 1




    It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
    – Joakim Danielson
    Nov 10 at 19:14










  • I have edit the code and make the method static
    – Hadeer Zayat
    Nov 10 at 19:26










  • I just need explanation for my question
    – Hadeer Zayat
    Nov 10 at 19:26










  • The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
    – John B. Lambe
    Nov 10 at 21:05
















  • 1




    Any specific reason why you made name and age static? (and the print() method not static)
    – SirDarius
    Nov 10 at 19:10








  • 1




    It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
    – Joakim Danielson
    Nov 10 at 19:14










  • I have edit the code and make the method static
    – Hadeer Zayat
    Nov 10 at 19:26










  • I just need explanation for my question
    – Hadeer Zayat
    Nov 10 at 19:26










  • The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
    – John B. Lambe
    Nov 10 at 21:05










1




1




Any specific reason why you made name and age static? (and the print() method not static)
– SirDarius
Nov 10 at 19:10






Any specific reason why you made name and age static? (and the print() method not static)
– SirDarius
Nov 10 at 19:10






1




1




It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
– Joakim Danielson
Nov 10 at 19:14




It is unclear what you are asking and your code is a mess, does it even compile? And try to follow java naming practices, it makes it so much easier for us that tries to read your code, class names should start with an upper case letter so Foo and S1 and also use more meaningful names for classes and variables than s1 and a
– Joakim Danielson
Nov 10 at 19:14












I have edit the code and make the method static
– Hadeer Zayat
Nov 10 at 19:26




I have edit the code and make the method static
– Hadeer Zayat
Nov 10 at 19:26












I just need explanation for my question
– Hadeer Zayat
Nov 10 at 19:26




I just need explanation for my question
– Hadeer Zayat
Nov 10 at 19:26












The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
– John B. Lambe
Nov 10 at 21:05






The class foo can access those static members of s1. The code given would not compile, because foo is misspelt as fue in main. If this was corrected, the output would be "ahmed 21" twice.
– John B. Lambe
Nov 10 at 21:05














2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can do something like this:



public class foo{
String s = s1.name;
public static void print()
{
System.out.println(s1.name + " " + s1.age);
}

}





share|improve this answer




























    up vote
    0
    down vote













    You have declared your variables as public static in class s1,



    public class s1 {
    public static String name;
    public static int age;
    ....
    }


    public means it can be accessed anywhere, and static means it can be accessed by just class name s1



    So no matter, whether in same package or any other package, you can always access those variables. So for example you can do something like this in foo class which is present in some other package,



    public class foo{

    static {
    s1.name = "new name";
    s1.age = 555;
    }

    public static void print()
    {
    System.out.println(s1.name + " " + s1.age);
    }

    }





    share|improve this answer























    • There's no package called s1 (at least not in the current edition of the question).
      – John B. Lambe
      Nov 10 at 21:04










    • You're right. Let me revert my answer then.
      – Pushpesh Kumar Rajwanshi
      Nov 10 at 21:08











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242450%2fhow-to-access-variables-in-another-package-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can do something like this:



    public class foo{
    String s = s1.name;
    public static void print()
    {
    System.out.println(s1.name + " " + s1.age);
    }

    }





    share|improve this answer

























      up vote
      0
      down vote













      You can do something like this:



      public class foo{
      String s = s1.name;
      public static void print()
      {
      System.out.println(s1.name + " " + s1.age);
      }

      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can do something like this:



        public class foo{
        String s = s1.name;
        public static void print()
        {
        System.out.println(s1.name + " " + s1.age);
        }

        }





        share|improve this answer












        You can do something like this:



        public class foo{
        String s = s1.name;
        public static void print()
        {
        System.out.println(s1.name + " " + s1.age);
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 19:11









        Charlie

        5442626




        5442626
























            up vote
            0
            down vote













            You have declared your variables as public static in class s1,



            public class s1 {
            public static String name;
            public static int age;
            ....
            }


            public means it can be accessed anywhere, and static means it can be accessed by just class name s1



            So no matter, whether in same package or any other package, you can always access those variables. So for example you can do something like this in foo class which is present in some other package,



            public class foo{

            static {
            s1.name = "new name";
            s1.age = 555;
            }

            public static void print()
            {
            System.out.println(s1.name + " " + s1.age);
            }

            }





            share|improve this answer























            • There's no package called s1 (at least not in the current edition of the question).
              – John B. Lambe
              Nov 10 at 21:04










            • You're right. Let me revert my answer then.
              – Pushpesh Kumar Rajwanshi
              Nov 10 at 21:08















            up vote
            0
            down vote













            You have declared your variables as public static in class s1,



            public class s1 {
            public static String name;
            public static int age;
            ....
            }


            public means it can be accessed anywhere, and static means it can be accessed by just class name s1



            So no matter, whether in same package or any other package, you can always access those variables. So for example you can do something like this in foo class which is present in some other package,



            public class foo{

            static {
            s1.name = "new name";
            s1.age = 555;
            }

            public static void print()
            {
            System.out.println(s1.name + " " + s1.age);
            }

            }





            share|improve this answer























            • There's no package called s1 (at least not in the current edition of the question).
              – John B. Lambe
              Nov 10 at 21:04










            • You're right. Let me revert my answer then.
              – Pushpesh Kumar Rajwanshi
              Nov 10 at 21:08













            up vote
            0
            down vote










            up vote
            0
            down vote









            You have declared your variables as public static in class s1,



            public class s1 {
            public static String name;
            public static int age;
            ....
            }


            public means it can be accessed anywhere, and static means it can be accessed by just class name s1



            So no matter, whether in same package or any other package, you can always access those variables. So for example you can do something like this in foo class which is present in some other package,



            public class foo{

            static {
            s1.name = "new name";
            s1.age = 555;
            }

            public static void print()
            {
            System.out.println(s1.name + " " + s1.age);
            }

            }





            share|improve this answer














            You have declared your variables as public static in class s1,



            public class s1 {
            public static String name;
            public static int age;
            ....
            }


            public means it can be accessed anywhere, and static means it can be accessed by just class name s1



            So no matter, whether in same package or any other package, you can always access those variables. So for example you can do something like this in foo class which is present in some other package,



            public class foo{

            static {
            s1.name = "new name";
            s1.age = 555;
            }

            public static void print()
            {
            System.out.println(s1.name + " " + s1.age);
            }

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 21:08

























            answered Nov 10 at 19:27









            Pushpesh Kumar Rajwanshi

            2,5831821




            2,5831821












            • There's no package called s1 (at least not in the current edition of the question).
              – John B. Lambe
              Nov 10 at 21:04










            • You're right. Let me revert my answer then.
              – Pushpesh Kumar Rajwanshi
              Nov 10 at 21:08


















            • There's no package called s1 (at least not in the current edition of the question).
              – John B. Lambe
              Nov 10 at 21:04










            • You're right. Let me revert my answer then.
              – Pushpesh Kumar Rajwanshi
              Nov 10 at 21:08
















            There's no package called s1 (at least not in the current edition of the question).
            – John B. Lambe
            Nov 10 at 21:04




            There's no package called s1 (at least not in the current edition of the question).
            – John B. Lambe
            Nov 10 at 21:04












            You're right. Let me revert my answer then.
            – Pushpesh Kumar Rajwanshi
            Nov 10 at 21:08




            You're right. Let me revert my answer then.
            – Pushpesh Kumar Rajwanshi
            Nov 10 at 21:08


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242450%2fhow-to-access-variables-in-another-package-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ