How to access String from one class to another? - java

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.

Related

Is there a way to take a string from inside a public static void and move it "outside" into a new class into a public static string?

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

Java: Access class within a method

I would like have one method declare two Strings and assign them values. Then have another method to read those values.
I have this structure:
public class ABC extends CDE implements EFG {
public void firstMethod(valueOne, valueTwo) {
class TestClass {
public String testValue = valueOne;
public String anotherValue = valueTwo;
}
}
public void readMethod() {
// HERE I WANT TO READ testValue AND ASSIGN IT A NEW VALUE
}
}
How can I do that?
And is there a better way?
Are you sure you need a class?
May be a simple field declaration will be enough:
public class ABC extends CDE implements EFG {
public String testValue;
public String anotherValue;
public void firstMethod(String valueOne, String valueTwo) {
// do whatever you wish with testValue and anotherValue
}
public void readMethod() {
// here you have access to both variables
}
}
Make the local class global and set/read values via instance:
public class ABC extends CDE implements EFG {
class TestClass {
public String testValue = valueOne;
public String anotherValue = valueTwo;
}
private testClassInstance = new TestClass()
public void firstMethod(valueOne, valueTwo) {
testClassInstance.testValue = valueOne
testClassInstance.anotherValue = valueTwo
}
public void readMethod() {
System.out.println(testClassInstance.valueOne)
System.out.println(testClassInstance.valueTwo)
}
}
All you want to do is to create a POJO that holds testValue and anotherValue and declare the class outside of your component class, e.g.:
class ExampleBean {
private String testValue;
private String anotherValue;
//getters and setters
}
Once done, you can hold the reference of that class into your component class and access the value, e.g.:
public class ABC extends CDE implements EFG {
ExampleBean exampleBean = new ExampleBEan();
public void firstMethod(valueOne, valueTwo) {
exampleBean.setValueOne(valueOne);
exampleBean.setAnotherValue(valueTwo);
}
public void readMethod() {
String value = exampleBean.getValueOne();
}
}
Maybe this will fit your criteria?
What you are currently asking for is impossible due to the scope of the inner class.
You could also initialize this private class instance in your constructor.
public class Sample {
class TestClass {
public String testValue;
public String anotherValue;
}
private TestClass localTest = new TestClass();
public void firstMethod(valueOne, valueTwo) {
localTest.testValue = valueOne;
localTest.anotherValue = valueTwo;
}
public void readMethod() {
localTest.testValue = "test1";
localTest.anotherValue = "anotherValue";
System.out.println(localTest.testValue);
}
}
You are declaring a class withing a method , which is not right
You need to understand what a class and a method really mean ( Google Java OOP ) :
1- You should create a class and declare the variables you want
2- make constructors for the default values
3- make setters to set (assign) these values
4- make getters to read those values

accessing a variable inside a method from another class using java

i want to access a variable inside another class
public class ephem_t{
public static void robel(){
int vflg;
Calendar t;
int iodc;
}
}
and i want to use the variables inside another class
public class testRobel{
public static void readfile(){
????????
}
}
i want to do like
public class testRobel{
public static void readfile(){
ephem_t eph = new ephem_t();
eph.robel.vflg = 1;
}
}
You should declare your class variables outside of a method, just
public class ephem_t {
int vflg;
Calendar t;
int iodc;
}
And an option is to use setters & getters to access those variables.
Or jus create a new object of your class.
public class testEphm_t {
public static void readfile(){
ephem_t eph = new ephem_t();
eph.vflg = 1;
}
}

How to import a String from a a different Class

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

How to read and write to variables of an abstract class

Put simply, I have an abstract class containing several variables and methods. Other classes extend this abstract class, yet when I try to read the private variable in the abstract class by calling getter methods inside the abstract class, it returns null as the value of the variable.
public class JavaApplication {
public static void main(String[] args) {
NewClass1 n1 = new NewClass1();
NewClass2 n2 = new NewClass2();
n1.setVar("hello");
n2.print();
}
}
public class NewClass1 {
public String firstWord;
public void setVar(String var) {
firstWord = var;
}
public String getVar () {
return firstWord;
}
}
public class NewClass2 extends NewClass1{
public void print() {
System.out.println(makeCall());
}
public String makeCall() {
return getVar();
}
}
Still prints out null.
Until the String is initialized, it will be null. You should probably have a constructor in the abstract class to set it.
public abstract class Command
{
String firstWord; // = null
protected Command(){}
protected Command( String w )
{
firstWord = w;
}
//...
}
public class Open extends Command
{
public Open()
{
this( "your text" );
}
public Open( String w )
{
super( w );
}
// ...
}
If you need to modify the firstWord string everytime execute() is called then it may not be necessary to use a constructor with a String parameter (I added a default constructor above). However, if you do it this way then either
You must make sure setFirstWord() is called before getFirstWord(), or,
Handle the case when getFirstWord() returns null. This could be by simply using a default value (maybe determined by each subclass) or something else, like failing to execute.
As I do not know all the details of your implementation I cannot tell you further information.

Categories

Resources