How to import a String from a a different Class - java

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");
}
}

Related

How to access a variable of another class and store it in a variable of the present class [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
class ClassB {
int c=0;
public static void main(String [] args) {
ClassA cla = new ClassA();
c=cla.getValue();
}
}
class ClassA {
int value = 0;
public int getValue() {
ClassA obj=new ClassA();
return obj.value;
}
}
I want to 'int value' of ClassA in 'int c' of class B. The above code shows the error "non static variable c cannot be referred from a static context". Please provide the correct coding for me as I am stuck.
Of course, becuase you can't call a non-static method without instantiating the object that contains this method fisrt, either make all the methods and fields static, or instantiate the class:
class ClassB {
static int c=0;
public static void main(String [] args) {
ClassB clb = new ClassB();
c= clb.getvalueA();
}
public int getvalueA(){
ClassA cla = new ClassA();
return cla.getValue();
}
}
class ClassA {
int value = 0;
public int getValue() {
return value;
}
}
Variable c in class B is not static and main block is static block that's why it showing error.You can not reference non static field from static context.
public class Main {
public static void main(String[] args) {
int retVal;
TestClass a = new TestClass(20);
retVal = a.value;
System.out.println("Value of a: "+retVal);
}
}
class TestClass {
int value;
public TestClass(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

How to access String from one class to another?

So for example I have 2 classes and one interface.
public class mainClass {
private String word;
}
public interface intWord {
public String getOutput();
public void setInput( String input );
public void setLevel ( int level );
}
public class secondClass implements intWord {
//class I want to be able to access 'word' from mainClass without extending it.
public String getOutput() {
}
public void setInput( String input ) {
}
public void setLevel ( int level ) {
}
}
How do I go about accessing "word" in my secondClass without extending mainClass to it? I believe I can add some helper methods somewhere to make it easy....but I can't figure it out!
In your mainClass you can add a "getter" method, as seen below
public class mainClass {
private String word;
public String getWord(){
return word;
}
}
And just call getWord() from your second class
You can also dynamically set your word value from your second class by adding a setter method:
public void setWord(String newWord){
word = newWord;
}
Try a getWord() and a setWord(String word)
Function Within mainClass and go from there.
One way to do assuming you want to use "word" as parameter for your setInput method will be to have an instance of mainClass and a getter to access the field word. Here is what it will look like:
public static void main( String args[])
{
// Assuming word is not null
secondClass second_class = new secondClass ();
secondclass.setInput(mainClass.getWord());
}
The other way will be to have your field word be sa static variable and return it with a static getter(Not recommended). Here is what you mainClass should look like:
public class mainClass {
private static String word;
public static String getWord(){
return word;
}
}
And here is how you will access it:
public static void main( String args[])
{
mainClass mainclass = new mainClass();
// Assuming word is not null
secondClass second = new secondClass ();
secondclass.setInput(mainClass.getWord());
}
The best way is to create setters and getters for private String word; in mainClass and create an object of mainClass in secondClass and access the getters and setters to get/set the value of word.
Another thing, if you know that word is global variable then you can create it as public string word; instead of private and extend mainClass in secondClass so as to access it directly.
Just for your knowledge java follows convention where class name should start with capital letter and then follow camel case.

JAVA get the value from one class to another

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());
}
}

Why can create non-static variable in the main but not outside the main. Java

Why when doing this, IDE says you need to change str1 and str2 to static:
public class Test {
String str1;
String str2;
public static void main(String[] args){
str1 = "A";
str2 = "B";
}
}
But this is fine:
public class Test {
public static void main(String[] args){
String str1;
String str2;
str1 = "A";
str2 = "B";
}
}
Why is it ok to declare a non-static variable inside a static method but not ok outside the static method?
Static method in a class only has reference to static member of the classes. "main" method is same as normal static method and follows the same rule.
For non-static members of a class, you must initialize an instance of the class firstly, then you can access the member.
public class Test {
String str1;
String str2;
public String getStr1(){
return str1;
}
public String setStr1(){
this.str1 = str1;
}
public static void main(String[] args){
//create an instance of the class firstly.
Test test = new Test();
// read and write the str1
System.out.println(test.getStr1());
test.setStr1("A")
System.out.println(test.getStr1());
}
}
public class Test {
String str1; //This is a variable that will be specific to each instance of the class Test
String str2; //This is a variable that will be specific to each instance of the class Test
public static void main(String[] args){
str1 = "A"; //This static method is not dependent on any instance
//so as far as a static method is concerned there is not str1
str2 = "B"; //This static method is not dependent on any instance
//so as far as a static method is concerned there is not str3
}
}
public class Test {
public static void main(String[] args){
String str1; //This variable is now independent of any instances of Test
String str2; //This variable is now independent of any instances of Test
str1 = "A"; //You can access those static variables from outside an instance
str2 = "B"; //You can access those static variables from outside an instance
}
}

Cannot compile inner class source files, resulting in error?

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

Categories

Resources