Java - metode inside a metode syntax issues [duplicate] - java

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 5 years ago.
Really don't know what it's called so I'm having a hard time searching for the answer.
Anyhow, I want to make a metode with metode inside (if that's even possible?).
public void log() {
public makeLogElement() {
//making a logelement to write inn
}
public write(String text) {
logelement.setText(logelement.getText() + text);
}
}
log myLog = new log();
myLog.makeLogElement();
myLog.write("This'll be written in the log");
What is the right syntax for making something like this?

It's not possible. But you can create a class inside a method.

Related

How to capture the error message in private function [duplicate]

This question already has answers here:
JUnit test for System.out.println()
(14 answers)
Closed 5 months ago.
I have a function compare which called private function compareRuleRanks, and println the error or correct message when condition met, how can I capture the message when I do unit test? I try to use AssertionError but didn't work, how can I do that?
AssertionError(dataCompare.compare("Rule1", "Rule2"));
public void compare(){
compareRuleRanks(rule1, rule2);
}
private void compareRuleRanks(rule1, rule2) {
if(rule1.rank != rule2.rank) {
println("The ranks are not in order");
}
println("rules are same");
}
When unit-testing compareRulesRanks, you can definitely call the function but you would have to measure whether the intended reaction was triggered.
That means you would have to check whether println() got called as expected.
If you cannot do that you probably have to improve your code for testability.

Accessing "this" from an anonymous Java subclass [duplicate]

This question already has answers here:
Getting hold of the outer class object from the inner class object
(7 answers)
Closed 6 years ago.
I'm trying to modify the behavior of JToolBar to allow it to dock to more than one JPanel. As part of this exercise, I need to override the method getDockingConstraint which I tried to do with an anonymous class using a definition very similar to the original.
The problem is that the original implementation references this several times which I thought would be fine, but I must be missing something because the IDE reports that this.dockingSensitivity is not visible to the anonymous class.
Is there a simple change here, or should I skip this approach and just create a full subclass of BasicToolBarUI? Or maybe there is a better approach entirely to modifying JToolBar's docking capability?
public MultiDockToolBar() {
setUI(new BasicToolBarUI(){
#Override
private String getDockingConstraint(Component var1, Point var2) {
if(var2 == null) {
return this.constraintBeforeFloating;
} else {
if(var1.contains(var2)) {
// Breaks here when using this.:
this.dockingSensitivity = this.toolBar.getOrientation() == 0?this.toolBar.getSize().height:this.toolBar.getSize().width;
if(var2.y < this.dockingSensitivity && !this.isBlocked(var1, "North")) {
return "North";
}
// Check East
// Check West
// Check South
}
return null;
}
}
});
}
dockingSensitivity is a private field inside BasicToolBarUI class. You will not be able to directly alter this. If you still want to edit and face the potential consequences, you can use Java Reflections library.

Why can I serialize attributes but not the object itself? [duplicate]

This question already has answers here:
Why does writeObject throw java.io.NotSerializableException and how do I fix it?
(4 answers)
Closed 6 years ago.
I am working on a little quiz tool and face problems when I want to persist my objects (questions). This is my save method within the class Question, which imports "java.io.*":
public static boolean saveQuestion(String file, Question q){
try{
FileOutputStream saveFile=new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(q);
save.close();
return true;
}
catch(Exception exc){
exc.printStackTrace();
return false;
}
}
This is how I call the method from another class:
Question q = new Question();
Question.saveQuestion("question.sav",q);
When I try to run it, it throws a "java.io.NotSerializableException" at the save.writeObject(q);
When I change my code in order to just store an attribute of the object it works fine. What can be the problem?
To serialize objects, your classes needs implements Serializable.

Writing code in javadoc comment [duplicate]

This question already has answers here:
Multiple line code example in Javadoc comment
(18 answers)
Closed 9 years ago.
Is it possible to write a short code inside a java doc comment?
I don't mean a simple line of code, I mean a short method like:
public static void main() {
doSomething();
}
You can put your code in Multiple line comments like this.
/*
* public static void main() {
* doSomething();
* }
/
If you want to be in Java documentation comment,then it would be with two * after slash
/**
* public static void main() {
* doSomething();
* }
/

Java replace line of output to standard out [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Write to same location in a console window with java
I was wondering if theres a way, in java, to replace a line of output you outputted to the terminal, such that you could do like a progress bar/counter type thing.
I'd like to do something akin to printing out "Records inserted 1/1000" and then "Records inserted 2/1000" over the top, replacing it so that only the most recent one shows.
Print the \r character, which places the cursor at the beginning of the line. And then write the new line.
public static void main(String[] args) throws InterruptedException {
System.out.print("test");
Thread.sleep(3000);
System.out.print('\r');
System.out.print("lulz");
}
Just rewire the System.out pipe to go through a filter of your own. e.g. System.setOut(new MyStream(System.out));
https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setOut-java.io.PrintStream-
You then need to implement MyStream:
public class MyStream extends PrintStream {
private PrintStream standardOut;
public MyStream(PrintStream standardOut) {
this.standardOut = standardOut;
}
... Then here override the appropriate methods (e.g. `println()`, etc...) to correct the output and send it to `standardOut`.
}

Categories