How can i get the value of string from class a to class b ?
public class A{
public String string = "A";
}
public class B{
public static void main(String []args){
System.out.printl(string);
}
}
You have to instantiate the class A and access the instance variable. Like this:
public class A{
public String string = "A";
}
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.string);
}
}
string is an instance variable of class A.
Firstly, you cannot have two public classes in the same file.
So I suggest that you move class A to A.java and leave theclass B in B.java
A.java
public class A{
public String string = "A";
}
B.java
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.string);
}
}
Upon invoking class B, a new object of class A would be create and the variable of A can be access through it.
public access to member fields cause so many problems, fix them before you have them with a technique of hiding them behind a method.
public class A {
public String getString() {
return "A";
}
}
public class B{
public static void main(String []args){
A a = new A();
System.out.println(a.getString());
}
}
Related
In a small example, I'm trying to take information from the String "helloFromMain" in a file called Main.java and move it "outside" the public static void into a public static string in a different file named data.java.
IN THE FILE MAIN.JAVA
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = helloFromMain;
}
Any help would be appreciated.
Also im relatively new to all this
You can set a public static variable from another class.
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
Data.helloFromData = helloFromMain;
}
}
Also, I've found that it's helpful to set a package for all classes, as it makes it simpler to manage importing and FS structure.
You can do it like this
Simple way.
IN THE FILE MAIN.JAVA
public class Main {
public static String helloFromMain; //declare a public static string, it will be accessible from outside the class.
public static void main(String[] args){
helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = Main.helloFromMain;
}
But be aware that Main.helloFromMain will be null until you call the main constructor.
Advance way :
IN THE FILE MAIN.JAVA
public class Main {
private static String helloFromMain; //This time the static variable is private, so you can't directly use it from outside
public static void main(String[] args){
helloFromMain = "hello";
}
//We create a public static method to access the private static variable
public static String getHelloFromMain(){
return helloFromMain;
}
}
IN THE FILE DATA.JAVA
public class Data {
//We call our public static method from Main.
public static String helloFromData = Main.getHelloFromMain();
}
You can create a static method in class Data to set the value of the string. If you do this, you don't need to make the variable in class Data public.
public class Main {
public static void main(String[] args) {
String helloFromMain = "hello";
Data.setHelloFromData(helloFromMain);
}
}
public class Data {
private static String helloFromData;
public static void setHelloFromData(String newValue) {
helloFromData = newValue;
}
}
It seems like PdCar class have been upcast to Car type, and I can't find a way to use methods that are not in Car interface. I'm just curious, is there a way to access readLabel method? Or it's just not possible?
Thanks.
interface Car{
}
public class Parcel5 {
public Car car(String s){
class PdCar implements Car {
private String label;
private PdCar(String whereTo){
label = whereTo;
}
public String readLabel(){ return label; }
}
return new PdCar(s);
}
public static void main(String [] args){
Parcel5 p = new Parcel5();
Car d = p.car("toyota");
}
}
It seems like PdCar class have been upcast to Car type
no.
If you want to access methhod readLabel() you have to declare it in the interface Car.
I can't find a way to use methods that are not in Car interface. I'm just curious, is there a way to access readLabel method? Or it's just not possible? Thanks.
interface Car{
String readLabel()
}
public class Parcel5 {
public Car car(String s){
class PdCar implements Car {
private String label;
private PdCar(String whereTo){
label = whereTo;
}
public String readLabel(){ return label; }
}
return new PdCar(s);
}
public static void main(String [] args){
Parcel5 p = new Parcel5();
Car d = p.car("toyota");
System.out.println(d.readLabel());
}
}
I have been trying to import a String from the classA to the classB by using constructor, but I don't get to use the String in the classB, why is that so?. The code:
class A:
class classA{
String A="THIS IS THE STRING";
String B="TEXTL: ";
public classA(){
this.A=B+A;
}
}
class B:
class classB extends classA{
public static void main(String[] args){
classA newclassA=new classA();
String Z=A; //WHY A IS NOT RECOGNIZED, WHAT DO I NEED TO DO?
}
}
This has to do with the scope of your variables. You can find more informatnion here.
Currently the variables stored in class A have package-private scope. There are really two ways to do what you are describing.
The better solution would be to provide a getter method within classA:
public String getA(){
return this.A;
}
This will access the A variable within the instance of the classA class. You can then change your main() to the following:
public static void main(String[] args){
classA newclassA=new classA();
String Z= newclassA.getA(); // Z = "TextL: THIS IS THE STRING";
}
Another option is to change the scope to protected to allow subclasses to access the variable field directly. i.e.
class classA{
protected String A="THIS IS THE STRING";
private String B="TEXTL: ";
public classA(){
this.A=B+A;
}
}
and
public static void main(String[] args){
classA newclassA=new classA();
String Z= newclassA.A; // Z = "TextL: THIS IS THE STRING";
// this allows you to access fields as if you were in the actual classA class.
}
I hope this helps.
public class stringName {
public String getString(String s){
s =" String in stringName class";
return s;
}
}
public class OutputString {
public static void main(String args[]){
String s = " ";
stringName sN = new stringName();
System.out.println(sN.getString(s) + " The string in the OutputString Class");
}
}
I have a sample code to solve which is based on inner classes:
package inner;
class A {
void m() {
System.out.println("Outer");
}
}
public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new A().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();
}
class A{
void m(){
System.out.println("Middle");
}
}
}
The output given by above sample code is:
Middle
Inner
And my question is, given that I dont want to use the package name to create an object, how can I print the output as:
Outer
Inner
Since using a package is so obviously the answer, I assume you are looking for something obtuse.
You can add an outer class and call that.
class B extends A { }
// in TestInner.go()
new B().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();
public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new inner.A().m(); //will produce output "Outer"
class A{
void m(){
System.out.println("Inner");
}
}
new A().m(); //will produce output "Inner"
}
class A{
void m(){
System.out.println("Middle");
}
}
The seem not to be able to compile the following main method:
public class MainMethod {
public static void main(String []args){
InnerizationClass outer = new InnerizationClass();
InnerizationClass.StaticInnerClass inner = outer.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
And this is the InnerizationClass class:
public class InnerizationClass {
String outerClassVar = "Outer Catism";
static String outerClassVarStat = "Outer Static Catism";
public static class StaticInnerClass{
String innerClassVar = "Catism";
public String getInnerClassVar(){
return this.innerClassVar;
}
public String getOuterClassVar(){
return InnerizationClass.outerClassVarStat;
}
}
public String locInnrMeth(){
class MethodLocalInnerClass{
String methodLocalInrCls = "Method Local Catism";
void printInner(){
System.out.println(InnerizationClass.this.outerClassVar);
}
}
return new MethodLocalInnerClass().methodLocalInrCls;
}
public String getStaticOuterVar(){return null;}
}
I am getting the following error from #1 from the main method:
MainMethod.java:6 qualified new of static class
InnerizationClass.StaticInnerClass inner = outer.new StaticInnerClass();
1 error;
Try this instead:
public class MainMethod {
public static void main(String []args){
InnerizationClass.StaticInnerClass inner = new InnerizationClass.StaticInnerClass(); //#1
System.out.println(inner.getOuterClassVar());
System.out.println(new InnerizationClass().locInnrMeth());
}
}
since your inner class is static you don't need to create an instance of InnerizationClass