Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
myClass1:
public class myClass1
{
public myClass2 myclass2;
public void createsecondclass (String[] args)
{
myclass2 = new myClass2(this);
myclass2.dosomething();
}
myClass2:
public class myClass2
{
public myclass1;
public myClass2(myClass1 myclass1)
{
this.myclass1 = myclass1;
}
public void dosomething()
{
myclass1.another_object_that_could_be_placed_here.dosomething();
}
}
would this not make the code cleaner when trying to access a large amount of objects which all in one way or another are all instantiated under a single class? i ask because i am trying to learn libGDX and in the large assortment of class files that are made to handle each element of the game it just seems easier to pass my application listener down the line since the application listener contains the screen which contains the gameworld which contains the player and so on...
But the problem that i worry about is that by setting it to a variable in the object i am creating a myclass1 that contains a myclass2 that contains a myclass1 and so on. i worry that this might cause memory leaks and since my target is android, memory is a big concern.
if anyone has any thoughts on the subject, directly relevent or not i would appreciate the input. i am after all still learning.
thanks =)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 days ago.
Improve this question
Let us say there is a class LargeClass with 4k lines of code. I would like to reduce the file size to manage it better by extracting the code into some other classes.
class LargeClass extends SomeLibraryClass {
void featureOneLargeMethod1() {
}
void featureOneLargeMethod2() {
}
void featureOneLargeMethod3() {
}
void featureTwoLargeMethod1() {
}
void featureTwoLargeMethod2() {
}
void featureTwoLargeMethod3() {
}
}
I could create two classes FeatureOne and FeatureTwo and move the corresponding methods into these new classes but the featureOneLargeMethod1() calls protected SomeLibraryClass.someLibraryMethod() which makes it impossible to move these methods. I was thinking of creating a wrapper interface that LargeClass implements and thereby FeatureOne and FeatureTwo class can access them. Any better approach in java or kotlin instead of creating the wrapper interface?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is there a way in java that interface can deliver function with already implemented part of code? e.g.
//somewhere in java classes
void function1(){
MyClient client = MyClient.getNew();
if(client.isReal()){
// function2 is an expansion for function1
}
}
//someInterface.java
public interface someInterface{
public void function2(MyClient client);
}
//someClass.java
public class someClass implements someInterface{
#Override
public void function2(MyClient client){
client.send("Hi!");
}
}
Maybe I didn't make it clear. I want to make an interface that delivers a function. No problem. But this function must implement some logic checks. How can I do that?
Yes, you can. You have to write a default method in your interface. Please go through this tutorial to find more details.
Java 8 has introduced the notion of "default methods", so you can definitively implement a method on an interface.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Say i have a class 'Test' which has number of static methods.
class Test {
public static T BytesToInt<T>(){
//Logic
}
public static void Parse(string data){
//
}
}
I create large number of instances of Test class. Will these objects be garbage collected?
Yes. As long as nothing is holding a reference to them they will be collected
Yes, when any Test instance out of scope or no longer referenced it would be eligible for GC. In general, instance means object created with constructor for example new Test(), and resides in heap area. Not to be confused with static members of which are class method.
Refer static method section in JLS below for more details:-
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.3.2
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to store input from one class into an array that is in a separate class method, but I'm not really sure how to do this.This is the class that is accepting the input that I want to store into the class method
This is the class my class method.
You should create your array as a static variable rather than a local variable.
public class State
{
private static String[] info = new String[5];
public static void store(State state) {
info[0] = ...
}
...
}
Another better option is to create State as Concrete class rather than a Static class, but you should crawl before you walk :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've been doing a large amount of studying lately, and I was working on C++, and the topic of the hidden pointer (this).
It is one of the most complex topics I have seen so far. I remember this from Java, and I don't remember anything about it being hidden in Java. I do remember that you have to explicitly use it in Java, but apparently it's automatic in C++. Can anyone confirm this?
Hidden? It's not hidden. What does that even mean?
I do remember that you have to explicitly use it in Java
Only in some circumstances. A variable called name could be known inside the class and also be the name of a parameter in a method. Example:
class Test {
String name;
public void test(String name) {
name = name; // What happens?
}
}
Both times name is mentioned it refers to the parameter. The class field is unchanged. You have to tell the compiler that you want this.name if you want the class field.
In other circumstances, when there are no collision in names, the this. part is implicit. Example:
class Test {
String tutorName;
public void test(String name) {
tutorName = name; // What happens?
}
}
The class field is changed even though you didn't use the this keyword.