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?
java oop encapsulation
add a comment |
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?
java oop encapsulation
1
Any specific reason why you madename
andage
static? (and theprint()
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 thans1
anda
– 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 classfoo
can access those static members ofs1
. The code given would not compile, becausefoo
is misspelt asfue
inmain
. If this was corrected, the output would be "ahmed 21" twice.
– John B. Lambe
Nov 10 at 21:05
add a comment |
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?
java oop encapsulation
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
java oop encapsulation
edited Nov 11 at 0:57
asked Nov 10 at 19:06
Hadeer Zayat
53
53
1
Any specific reason why you madename
andage
static? (and theprint()
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 thans1
anda
– 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 classfoo
can access those static members ofs1
. The code given would not compile, becausefoo
is misspelt asfue
inmain
. If this was corrected, the output would be "ahmed 21" twice.
– John B. Lambe
Nov 10 at 21:05
add a comment |
1
Any specific reason why you madename
andage
static? (and theprint()
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 thans1
anda
– 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 classfoo
can access those static members ofs1
. The code given would not compile, becausefoo
is misspelt asfue
inmain
. 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
add a comment |
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);
}
}
add a comment |
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);
}
}
There's no package calleds1
(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
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
You can do something like this:
public class foo{
String s = s1.name;
public static void print()
{
System.out.println(s1.name + " " + s1.age);
}
}
answered Nov 10 at 19:11
Charlie
5442626
5442626
add a comment |
add a comment |
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);
}
}
There's no package calleds1
(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
add a comment |
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);
}
}
There's no package calleds1
(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
add a comment |
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);
}
}
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);
}
}
edited Nov 10 at 21:08
answered Nov 10 at 19:27
Pushpesh Kumar Rajwanshi
2,6161821
2,6161821
There's no package calleds1
(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
add a comment |
There's no package calleds1
(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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Any specific reason why you made
name
andage
static? (and theprint()
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
anda
– 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 ofs1
. The code given would not compile, becausefoo
is misspelt asfue
inmain
. If this was corrected, the output would be "ahmed 21" twice.– John B. Lambe
Nov 10 at 21:05