Click here to Skip to main content
15,557,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this class,
public static void smallCollegeNortheast()
      String c1 = "Cheyney University of Pennsylvania";

and in another class I am trying to figure out how to get c1 and assign it to a new string as a "save" feauture (So when I call for it, it prints all of the outputs the user "saved"). I tried making a new String and assigning it put that doesn't work.

String c2 = c1;
if(c2 != )

What I have tried:

Wht I have tried is above. Is what I'm trying to do possible?
Updated 24-Nov-21 20:12pm

First, what you posted is not a class. It's a method. It cannot exist outside of a class, which you haven't shown.

Next, you cannot expose a variable that is defined in a method to any code outside of the method. There is no way for any code outside the method to see that variable.

You CAN, however, expose data like that through Properties[^].
Share this answer
Member 15427218 12-Nov-21 9:51am    
do you want me to post the whole code?
Dave Kreskowiak 12-Nov-21 10:00am    
Hover your mouse over your question above and click the "Improve question" widget.

Add the relevant code.
do you want me to post the whole code?

No, because what you want to do isn't possible: c1 is a variable that is defined inside the method smallCollegeNortheast. That is called a local variable and it only exists in the scope of the method body.

Scope defines how "visible" an item is: it is limited to the enclosing set of curly brackets and cannot be used outside them unless it is declared public (or similar) and defined at class level.

In the case of local variables, it's even worse: the variable is created when the method is called, and destroyed when it returns - literally destroyed, the memory space that was allocated on the stack to hold it is released and becomes available for other functiosn to use!

The only way to preserve the value once the method exits is to either store it in a class level variable or return it from the method to the caller wirth a return statement - in which case the method must be declared to return teh appropriate value type, not void
Share this answer
The variable c1 is local to that method (not a class) and only exists until the System.out.println statement completes. Once that method returns the variable no longer exists. So you need to reword your question to explain exactly what you are trying to do.
Share this answer
To access the variable of another class, you have to create an object of the called class in the caller class.

class Bigbang{
    public void galaxy()
Public class Universe{
    Bigbang bigbang=new Bigbang();

You can also use the getter and setter method . Without having any instances to create, you can declare multiple objects in the same class.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900