I have this in my Main class in java
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
int aaa=0;
System.out.println(aaa);
}
}
I Want to Override that toString() Method that implicity called.
why output is 0 not "lol" ?
why output is 0 not "lol" ?
because you are printing an integer and not an instance of that Main class
you can do the following
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
// int aaa=0;
Main myMain = new Main();
System.out.println(myMain);
}
}
note that you can do
System.out.println(myMain);
the same as
System.out.println(myMain.toString());
Related
I'm writing a program for my class and i'm getting compiling errors and I'm not sure why. I think it has something to do with my Test method.
public class Test1 {
public static void main(String[] args) {
String test;
public void Test(String s){
text = s;
}
Test test = new Test("ABC");
System.out.println(test);
}
}
public class Test1 {
String text;
public Test1(){
}
public Test1(String s){
this.text = s;
}
public static void main(String[] args) {
Test1 test = new Test1("ABC");
System.out.println(test.text);
}
}
Try this..
I need to pass a string from class to another class in Java (Bukkit), I have already read some similar questions, but I can't solve the problem.
I have a Main class
public class Main extends JavaPlugin {
#Override
public void onEnable() {
new PlayerListener(this);
this.saveDefaultConfig();
String bannedBlocksString = this.getConfig().getString("bannedBlocks");
}
#Override
public void onDisable() {
}
}
And another class "PlayerListener"
public class PlayerListener implements Listener {
public PlayerListener(Main plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
// public static final String bannedBlocksString = "DIAMOND_BLOCK; EMERALD_BLOCK";
public static final String[] bannedBlocks = bannedBlocksString.split("; ");
public static boolean isBannedBlock(String[] bannedBlocks, String blockPlaced) {
boolean returnValue = false;
for (String bannedBlock : bannedBlocks) {
if(blockPlaced.equalsIgnoreCase(bannedBlock)){
returnValue = true;
}
}
return returnValue;
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
String blockPlaced = event.getBlockPlaced().getType().toString();
if(!event.getPlayer().hasPermission("antibuild.block.noplace") && isBannedBlock(bannedBlocks, blockPlaced)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You can not place this block.");
}
}
}
How can I get the value of bannedBlocksString in Main from the class "PlayerListener"?
Try this, I hope it works:
From Main:
PlayerListener pl = new PlayerListener(this);
this.saveDefaultConfig();
String [] bannedBlocksString = pl.getBannedBlocks();
From PlayerListener you have to declare get method:
public String [] getBannedBlocks(){
return this.bannedBlocks;
}
If you uncomment the bannedBlocksString in the PlayerListener then you can always access it in the Main class using PlayerListener.bannedBlocksString as the variable is static.
If you want to do it the other way arround and assign the value you need to remove the final from the variable and use the code beneath.
PlayerListener.bannedBlocks = bannedBlocksString.split("; ");
For example, I have such code:
SomeClass item = new SomeClass();
OtherClass.someVoidMethod(item); //there was some changes with item
and then in class, where was called method someVoidMethod(item) item will not change, cause we use in this method copy of that item. In C/C++ there is pointers. I'm looking for something like that in Java.
there is better example:
There i cant edit string from method
public class Main {
public static void main(String[] args) {
String string = "String";
changeIt(string);
System.out.println(string);
}
public static void changeIt(String string){
string = string + " edited";
}
And what suntax I need, to do that? I tied to use *string and so on, but i do this wrong.
i find that this code wil not work the way i want
public class Main {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
OtherClass.changeIt(someClass.getValue());
}
}
class SomeClass {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class OtherClass {
public static void changeIt(String string){
string = "someStr";
}
}
so i need to use something like that:
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
someClass.setValue(OtherClass.changeIt(someClass.getValue()));
}
public static String changeIt(String string){
return "someStr";
}
and there is no other way?
To make a Long Story short: use Parameters as Inputs to a method, and use the return value as Output. This will make your code better readable and easier to maintain.
For example:
public class Main {
public static void main(String[] args) {
String string = "String";
String modifiedString = changeIt(string);
System.out.println(modifiedString);
}
public static String changeIt(String string) {
return string + " edited";
}
}
If you wrap your string into a mutable Container, than you can modify it in your method. However for your example code I would strongly recommend to not use this Approach!
class StringContainer {
String content;
}
public class Main {
public static void main(String[] args) {
StringContainer container = new StringContainer();
container.content = "String";
changeIt(container);
System.out.println(container.content);
}
public static void changeIt(StringContainer container) {
container.content += " edited";
}
}
The StringBuilder, as mentioned by #aioobe, would also be a good alternative to use.
i have following code in which the base class Employee have a static method meth1() which i am able to call from a child class (Pro) object . Is it a case of method hiding or what ? , i am not sure because i haven't implemented the meth1() method in Pro class, but still able to call Emplyee static method from Pro object.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
/*
* public void meth1()
{
System.out.println("inside encapsulation-meth1");
}
*/
public static void main(String as[])
{
Pro e = new Pro();
// e.s ="jay";
e.meth1();
}
}
Output:
inside emp-meth1
Thanks
Jayendra
What are you trying to hide?
Try the below code
emp.meth1() will call method based on reference not based on the object being referred.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
protected static void meth1()
{
System.out.println("inside encapsulation-meth1");
}
public static void main(String as[])
{
Pro e = new Pro();
Employee emp = new Pro();
emp.meth1(); //this is case of method hiding
e.meth1();
}
}
The code cannot be compiled:
public class TestBench_1_2_Answer {
public static void reverse(char* str) {
}
}
The error is:
Syntax error on token "*", delete this token
In Java you don't have pointers, you pass the String.
This will work:
public class TestBench_1_2_Answer {
public static void reverse(String str) {
}
}
it should be
public class TestBench_1_2_Answer {
public static void reverse(char str) {
}
}
what are you trying to achieve with * with type in java
if your intention is to reverse the string
public class TestBench_1_2_Answer {
public static void reverse(String str) {
new StringBuilder(str).reverse().toString();
}
}