Function redefinition's legal in java? - java

Eclipse allows me to run this by simply pressing a proceed button in a "Error in work-space" dialog:
class MainClass
{
public static void foo()
{
}
public static void foo()
{
}
public static void main (String[] args)
{
}
}
Does this mean that function redefinition's like these are legal?

Eclipse allows you to run code which doesn't compile. It replaces such code with throw new Error("whatever the compilation error was"); I consider this a very BAD (Broken As Designed) feature but others love it.
In your case this works because you don't use the function anyway.

no it is illegal, it must differ in method signature

Two methods are prohibited from having the same signature this won't compile.

check again, you should get following error
Duplicate method foo() in type MainClass

Related

Cannot find symbol using System.out.println(""); (NetBeans, IntelliJ or Eclipse)

Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.

How to create a function that takes print method reference as argument?

I want to create a method that takes print method reference as argument. Is there any way to create a method like below?
public static void main(String [] args) {
runFunction(System.out::print);
}
public static void runFunction(MethodRef methodRef){
methodRef("test");
}
EDIT:
I have created a functional interface like;
public interface Generatable<T> {
void generate(T t);
}
And I updated my runFunction as;
public static void acceptFunction(Generatable generatable){
generatable.generate("test");
}
this method works perfectly but I still don't get how it works. I mean how it calls print method when I call generate("test").
Yes, like Prashant said.
But if you use a functional interface you better add a generic type like that:
public static void runFunction(Consumer<String> consumer){
consumer.accept("test");
}
For more functional interfaces you can take a look at https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
Yes, you can use a Consumer as:
public static void runFunction(Consumer consumer){
consumer.accept("test");
}
Side note: Functional programming is only supported with Java 8 +. So if you are using any version of Java below 8, this will not work and there is no simple way of fulfilling your requirement.

Why error "cannot find symbol class out" occur?

I was writing a code in which I print a statement "Hello World" but an error occur named cannot find symbol. I tried hard to remove this error but failed.
public class Input{
System.out.println("Hello World");}
This is the statement and the error
Please anyone can help me in resolving this error and tell me why this error occur so I will not repeat this mistake in future.
Use this :
public class Input {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
You can't print in the scope of a class. You need to inform more about the difference between a function and a class.
A "class" is sort of like a noun. It's the person, place or thing.
public class myThing {
//This is a comment
//put stuff that describes the thing in the curly braces
}
In order to make the thing do something, you must put it in a method
public void myMethod(){
//code in here gets run when myMethod runs
}
Methods must be in a class though. A method can usually be though of as "something a thing can do"
public class myThing {
public void myThingCanDoThis(){
//does this stuff only when called from somewhere else
}
}
The main method is a special type of method that always looks like this:
public static void main(String[] args) {
//This special method gets run automatically when your program runs
}
Your hello world should define a thing. It should have the method, or "verb", of "main".
public class myThing {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For now just assume that all your java code must go inside of the main method. Learn the basics and then when you learn more start to pay attention to things like "public", "static", "class" , "void" etc. It will all make sense in time

why compilation fails for that example

I was working on SCJP6 dumps when I found this confusing exercise:
Given classes defined in two different files:
package packageA;
public class Message {
String getText() { return “text”; }
}
And:
package packageB;
public class XMLMessage extends packageA.Message {
String getText() { return “<msg>text</msg>”;}
public static void main(String[] args) {
System.out.println(new XMLMessage().getText());
}
}
What is the result of executing XMLMessage.main?
A. text
B. Compilation fails.
C. <msg>text</msg>
D. An exception is thrown at runtime.
The answer was: B, but I don't understand why; I think the answer should be C.
If the code you posted it's the one that is in the book, the correct answer as you mentioned is C, let me explain why.
Again, assuming you copied the code as it's shown in the book when you do, the following line:
String getText() { return “<msg>text</msg>”;}
Its not overriding the getText() method in packageA.Message class but declaring a new one, that will can be accessed for XMLMessage instances within packageB.
This would be different if the the main method is something like:
public static void main(String[] args) {
Message message = new XmlMessage();
System.out.println(message.getText());
}
In this case there is a compilation error since the Message.getText() methods is not exposed outside the package.
A package default method cannot be overridden because it is not visible in another package.
In your example, method getText() in class Message is only visible to members of packageA.
Method does not override package visible method in Eclipse
The method String getText() { return “text”; } is with package (default) scope . And hence it is not visible outside the package packageA .
so it is not possible to override the method in the class XMLMessage which is outside the packageA .
You can learn the basics of method overloading and overriding here

Java - how to load method inside class it is defined?

For example I have this code:
public class A
{
private void my_method(){
//do something
}
}
So how can I call that method for code below to use it?
I saw in one example it was done like this:
public class A
{
public A {
my_method();
}
//some other code
private void my_method(){
//do something
}
}
But trying this gives me this error:
"Syntax error on token "public", class expected after this token"
And of course using advise in error, gives this error:
"The nested type A cannot hide an enclosing type"
So it seems that code I saw is bad or somehow I'm doing something wrong. Anyone could explain how to do it properly in Java?
Your constructor is wrong (you forgot the brackets).
It has to be
public A() {
}
constructor is missing (). use
public A()
{
}
You are getting this error because you have not wrote the constructor correctly.
It should be:
public A() {
my_method();
}
Just to expand on Jeroen's answer as it seems you are quite new to Java:
Your private method can be called from inside another method in your class. E.g.
public class A
{
public void anotherMethod() {
my_method();
}
private void my_method(){
//do something
}
}
The code you provided was called within the constructor of the class. This is a special method which is called when an object of type A is constructed e.g. new A();. You can tell it's a constructor because it has no return type specified:
public A() {
}
rather than a normal method:
public void a() {
}
Something to note there is that in Java it is convention (but not strictly required) to name normal methods with a lowercase first letter and classes/objects/constructors with an upper case first letter.
So your mistake was that in your constructor you had not put () after the method name (A in this case).

Categories

Resources