Imagine this extreme situation: A class City with 10 variables, but 1000 lines of functions and logic that operates these variables. I don’t know exactly how much memory will occupy a instance of this class, but let’s say that the variables occupy 10 bytes and the 1000 lines of functions occupy 1 kb to put a sample amount. If you
Tag: class
How can i solve the problem with creating a class in java using Eclipse
So I am using eclipse to practice coding using java and till now it was doing great, until I wrote this program: whenever I am trying to run the code it is showing me nothing. I need help. Answer It wont show anything. Your code will wait till you enter the care name. So, first you need to provide input
How to create a list of class names and iterate throught the static methoc in it?
I have list of classes: List<Class<?>> = Array.asList(ClassA, ClassB, ClassC, ClassD); each of them has a static method staticMethod(byte[] bytes) How can I iterate through this list of classes and calling the staticMethod? Answer Represent your List like lambda expressions for your static methods. Full code: Output: EDIT: Example for methods with return value:
“grade” cannot be resolved to a variable
I have this exercise and the question is how can I solve the error “grade” cannot be resolved to a variable, without declare it in the class teacher. I suppose is the only error in my code. It is evident for me WHY then in my output only the grade variable is not assigned, but I don’t know HOW to
How to have the program regenerate a “new game” where previous grid letters get swapped with new grid letters
In this program I’m trying to input a functionality to start game, where if I click start game (MainMenu) a new jpanel opens up (MainGame), creating another jpanel that creates jbuttons in a grid. If i go back, and click start game again, a new grid should generate instead of the previous one, effectively starting a “new game”. the problem
Parameter to pass in for type Class
I am trying to make a generic Stack class as shown but am unsure of what to put in for type when instantiating since I thought of doing however I get the following error of so I thought of using but I am unsure of what to put inside of cast() since it won’t take now I am stuck. Answer
Compare two objects excluding some fields – Java
I need to compare two objects of the same class excluding some fields. How can i find if the two objects of the above class are equal excluding createdAt and updatedAt values? Since there are a lot of fields in this class, i don’t want to compare each of them one by one. Please don’t give AssertJ’s recursive comparison solution
I have a HashSet pre. Is there a fast way to do HashSet PreIDs = for each x do x.getID in Pre? (in Java)
There is a class Prerequisite, it has a method getID(). Instead of doing is there a more efficient or more concise way to call a method over a HashSet? Answer As @ernest_k said, there isn’t any more efficient way in my opinion too. But we can write that whole logic in one line as below (if you are using Java
Replace class methods with lambda expressions, is it bad practice?
I had a few questions that I’m having trouble finding the answer to, at least in Java. I saw a Class definition in a tutorial where lambdas were being used similar to methods. So I’m curious as to if there is any benefit apart from concise code and style preference. Example: Is this a valid class definition? Is this a
Call method in Fragment from some class
I’ve been working on Android App in AndroidStudio. Application has Fragment, let’s call it MainFragment, and seperate Java Class file, let’s call it SQLiteControler.class The problem is when i try to call method from Fragment in SQLiteController im getting null object reference as error Here is some minified code: MainFragment SQLiteController Note: This is minified code I have tried, a