• Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving massive elements from class to class  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


These both classes is in diffrend files.
How to 'Piestuku class' massive first & second element and int flom load to 'class Sandelys' ?  Then. In the 'class Sandelys ', once I "run" it. I want to see in console: massive first & and second element, int flom.


I attached the file.
png.png
[Thumbnail for png.png]
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch 

That array is written as a local variable inside the main method and it has no existence outside that method. You will have to move it out of the main method, and provide methods which allow it to be accessible from other code.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch 

That array is written as a local variable inside the main method and it has no existence outside that method. You will have to move it out of the main method, and provide methods which allow it to be accessible from other code.


Can you write me a code? Because I cant understand what to do what you have said.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps the Oracle tutorial about the different variables types is a good place to start...

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Henry

 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Foo, i is a local variable in method1, but doesn't exist in method2. In Foo2 i is a private field, which exists throughout the whole class.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to transfer 'int ui= 10' from class Rytas, to the class Rytinis. Wheres the problem?


public class Rytinis {

public static void main(String[] args) {

Rytas ry = new Rytas ();
ry.metodas ();
System.out.println (u);

}

  }  

class Rytas {

void metodas()
{ int ui = 10; }
   }
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
picture.
rytinis.png
[Thumbnail for rytinis.png]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make the method 'metodas' return the value instead of void, and then save the result of the method in the main method.

This Oracle Java tutorial explains how to do that: Returning a Value from a Method
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Make the method 'metodas' return the value instead of void, and then save the result of the method in the main method.

This Oracle Java tutorial explains how to do that: Returning a Value from a Method


Looking into oracle docs ends in more confusion. Because I dont understand anything.
Finish the code please.

public class Rytinis {

public static void main(String[] args) {

Rytas ry = new Rytas ();
ry.metodas ();
System.out.println (ui);

}

  }  

class Rytas {

int metodas()
{ int ui = 10; return ui;}
   }
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Looking into oracle docs ends in more confusion. Because I dont understand anything.
Finish the code please.


Sorry, that is not the practice here. Coderanch is a learning site. The goal is to help you learn -- and not do your homework.


And I am sure that "don't understand anything" is not true. After all, how did you write your current code in the first place? ... What is it about the Oracle docs that is confusing you? Do you have something specific you need elaborated?

Henry
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it looks like this topic is just the exact same issue as your other topic...

https://coderanch.com/t/672061/java/Moving-massive-elements-class-class

So, please continue the discussion there.



Update: Since the two topics have been merged, the link above is now a circular link to the same topic. And the "continue the discussion there" statement no longer makes sense ... ... never mind.

Henry
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . it looks like this topic is just the exact same issue as your other topic... . . .
Nearly the same; it started off being unable to pass something from the main method to another method, and now it is inability to pass something from another method to the main method. But they are so similar that I think it is worth merging the two discussions.
I think the real problem is that too much is being done in the main method, and everything shou‍ld be done elsewhere. The main method is intended for starting the application off, and that link shows examples of how it shou‍ld be done. You need a class which encapsulates the data you are using, and methods in that class to manipulate those data.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all if someone thinks, that i put homework, im not. Because I gain no homework, I just do custom practice.
I dont use oracledoc because I have problems to understand that code. Since english is not my native, and im very beginner programmer.



public class One {
public static void main(String[] args) {

float a = 5;            //This class is main class

Two.method ();

System.out.println (a+ui);              // as you can see, I want that console would count a+ui =15.

/*of course I could just copy and paste "int ui = 10" to main class. But I dont want,
                  because this is with diffrend purpose. I want "transfer" variable ui from 'class two'    to    ' class one'.   */

}
}


class Two {

int method ()
{ int ui = 10; return ui; }

}



 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
/*of course I could just copy and paste "int ui = 10" to main class. But I dont want,  because this is with diffrend purpose. I want "transfer" variable ui from 'class two'    to    ' class one'.   */


Two possible answers.

First answer. Simply, with local variables, you can't. Local variables have a scope of the block that it is declared in. This means that it is impossible for a local variable to have a scope of more than one method. Additionally, there is no mechanism in Java to change the scope -- or to "transfer" a local variable from one method to another.

Second answer. There are other types of variables -- such as instance variables or static variables. Instance variables have a scope of a particular instance, and static variables have a scope for a particular class. Since these scopes can be made to encompass method calls, you can use these types of variables. Specifically, the two methods are not sharing or "transferring" each other's local variables -- but instead, there are using the same instance or static variables that both methods can access, and hence, share.

Henry
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite hard for me to comprehend.
But there is things such as "polimorphism' and "composition".
If you can do with 'static', I dont mind it is fine. I will be glad if you write code.
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:But there is things such as "polimorphism' and "composition".
If you can do with 'static', I dont mind it is fine.


Here is the Oracle tutorial on variables.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

The first two types, instance and static, are the two types that can be shared between methods.

Also, you don't need to understand polymorphism or composition concepts for this.

Henry
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what static means? If you don't know what static means, assume every use of static is a mistake (except for the main method which has to be static).
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will doubtless learn about things static in a few weeks. So, for the time being, the keyword static is only used for the main method heading, and your main method will read something like this:-Obviously you will need a class appropriately named with a start() method. You will doubtless change all the names I wrote. Read more about the main method here. What I showed is an abbreviated version of what Winston shows in that FAQ. There is another similar way to do that, with the start() method being in the same class. You will now need a class of the appropriate name which doesn't need a start() method:-
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find out.
public class Return {

public static void main(String[] args) {
Pastetas p = new Pastetas ();
System.out.println ( p.graza() );
System.out.println ( p.mas() );
}
}

class Pastetas {

int graza ()
{int c= 5; return c;}


int mas ()
{ int o = 1;
int k = 7;
return k; }
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!