i have coded....
public class mystring{
public String concaT(String s1,String s2){
s1=s1+s2;
return s1;
}
public static void main(String[] args){
String s="stack at";
mystring obj=new mystring();
System.out.println(s.concaT("concat"));
}
}
the thing is, the main method is taking input concaT(s,"concat"), but i want to use s.concaT("concat"). how to make it possible???
s is and object of type String. To do s.concat(String) you have to implements this method into String.class but this isnt possible because String is a final class that cant be extended otherwise you can creat a subclass of String and implement the method there. Why you did not want to use the String.concat(String) which is still implemented in String class?
String class is final defined in library, you can not make sub class it so you can not add any new method to it.
String#concat(java.lang.String) is already available in library use that.
s.concat("concat")
Make it static:
public class mystring{
public static String concaT(String s1,String s2){
s1=s1+s2;
return s1;
}
public static void main(String[] args){
String s="stack at";
mystring obj=new mystring();
System.out.println(concaT(s, "concat"));
}
}
If you want to use it outside the class mystring you must either call it by mystring.concat ("first", "second") or insert an import static mystring.concaT at the beginning of the file to make Java know that you mean that method:
import static mystring.concaT;
// ...
class OtherClass {
public void someMethod () {
String s = concaT ("first", "second");
}
}
Related
Is it able to avoid using "static" when call variable from another class? thank you very much
Here is my code.
class Hello {
public static String say = "Hello World"; //I using static
public void born() {
System.out.println(say);
}
}
public class SayHello extends Hello {
public static void main(String[] args) {
Hello myHello = new Hello();
myHello.born();
System.out.println(say);
}
The Output:
Hello World
Hello World
If I use public String say = "Hello World";
it output Hello World null
AnyIdea to avoid using "static" when call variable from another class?
thank you very much
If you remove the static, it will not compile. Static fields can be marked private, if you want to hide them. So then they are reachable by all instances of the class Hello only. The proper way of modifying or getting would be:
class Main extends Hello {
public static void main(String[] args) throws Exception {
Hello myHello = new Hello();
myHello.born();
// System.out.println(say); //doesn't allow access
// System.out.println(Hello.say); //doesn't allow access
System.out.println(myHello.getSay());
}
}
class Hello {
private static String say = "Hello World"; //private
public void born() {
System.out.println(say);
}
public String getSay() {
return say;
}
}
A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class.
So if you don't want to use static, then you can't use it in the other instances of class.
Yes, if you don't declare it static you can reference it from an instance: myHello.say.
It is the same as for calling a function.
public class SayHello extends Hello {
public static void main(String[] args) {
Hello myHello = new Hello();
myHello.born();
System.out.println(myHello.say);
}
}
For a constant, ie. a String that never changes and is the same for all instances of the class, it makes sense to declare it static and use it as such.
If you don't mark the string as static you will get a compilation error because when you do System.out.println(say) in the main method you are using say in a static context (since the main method must be static).
If you remove System.out.println(say); and just leave myHello.born(); then there's no need for say to be static because you'll only be using it from non-static methods (i.e. the born() method). You can see it in this example where I commented that line and defined say as not being static.
Another option would be to make the println like this, since the variable is public: System.out.println(myHello.say);
It is all from a question my friend give me:
[???]
public class Exercise{
public static void main(String[]arg){
assert ("Hello "+new A()).equals("Hello world");
}
}
Is there any way to put something on the question mark areas to make it work? In my cognition, we can only define a method inside class A then call the method like new A().method() to return the String result? This is really confused me! Sorry but did not code Java for a long time haha.
When a parameter should be a String reference, but is a reference to another type of object, Java calls the object's toString() method to create a String and then uses the resulting String reference.
public class A {
public static void main(String[]arg){
assert ("Hello "+ new A()).equals("Hello world");
}
#Override
public String toString() {
return "world";
}
}
Since you are asking about something that can be written above the class, your friend might be referring to a annotation of some library such as LOMBOK. You can checkout few examples here.
A constructor, doesn't return.
However, if you really want to do that, you can do it like this:
You can add a callback as parameter and call it with expression that you want to return.
Provide a dummy string as parameter and overwrite its value inside the constructor.
You can override the toString() method of Object's class. Something like this
public class Exercise{
#Override
public String toString() {
return "Your String";
}
public static void main(String[]arg){
assert ("Hello "+new A()).equals("Hello world");
}
}
class A {
public String toString() {
return "wo"+"rld";
}
}
You are welcome.
I'm creating a project that need to pass string from first program to second program but i need to pass string in main method of first class. I've googled but i cannot find what i need, mostly people use setter and getter to pass string between class but i cannot do that in main method.
How to pass string in main method of another class?
what i need is shown here:
public class FirstProgram{
public void first(){
String a = "hello";
}
}
public class SecondProgram{
public static void main(String[] args){
//i need to pass string here
}
}
Its not a good approach to take, but technically you can call the main method of the Second program and pass whatever argument you like
public class FirstProgram{
public void first(){
String a = "hello";
SecondProgram.main(new String []{a});
}
}
You can do this:
First.java:
public class First {
public static void main(String[] args) {
Second.main(args);
}
}
Second.java:
import java.util.Arrays;
public class Second {
public static void main(String[] args) {
Arrays.stream(args).forEach(System.out::println);
}
}
If you execute java First foo bar baz bat on the command line, you'll see that Second prints out "foo bar baz bat" in the console.
I don't recommend it.
You have to start your app by calling main in some program. Your First can invoke main in Second as shown, but it's First that starts the process.
Pretty confusing question.
But you can do smth like that:
public class FirstProgram{
public String first(){
String a = "hello";
return a;
}
}
public class SecondProgram{
public static void main(String[] args){
FirstProgram firstProgram = new FirstProgram();
String result = firstProgram.first();
}
}
package java;
//----------------------------- add one more line in here
class Demo {
public static String prt(String name) {
return "my name:" + name;
}
}
public class Sample {
public static void main(String[] args) {
System.out.println(prt("hong"));
}
}
if there is any way to print
my name : hong
,please let me know.
You can do this by creating an instance of the Demo class with new Demo(), like this:
class Demo {
public static String prt(String name) {
return "my name:" + name;
}
}
class Sample {
public static void main(String[] args) {
System.out.println(new Demo().prt("hong"));
}
}
You should be able to reference it via the class name:
Demo.prt("Hong")
however, if you can't use class Demo, then I'm not exactly what real life situation this would be. Methods belong to classes, they do not exist on their own. They must be referenced by classes, or instances of the class. Unless you're providing more context on your question, the answer is No.
However, if you can reference it by object, you could do this:
Demo demo = new Demo();
System.out.println(demo.prt("hong"));
public class DialogBox {
public static void main (String arg[]) {
String inputCourseCode;
inputCourseCode = this.inputCourseCode();
}
public String inputCourseCode() {
String input = JOptionPane.showInputDialog("Input the course code of this course:");
return input;
}
}
How to call the method inputCourseCode in main function?
You need to have an instance of DialogBox in order to call the inputCourseCode method.
For example:
public static void main (String arg[])
{
String inputCourseCode;
DialogBox box = new DialogBox();
inputCourseCode = box.inputCourseCode();
}
main is a static method; consequently, it does not have access to a 'this' reference.
It's an instance method, so you need an instance of DialogBox to call the method.
public static void main (String arg[]) {
DialogBox foo = new DialogBox();
String inputCourseCode = foo.inputCourseCode();
}
It needs to be static
public static String inputCourseCode()
then within Main you remove the this.
public static void main (String arg[]) {
String inputCourseCode;
DialogBox d = new DialogBox(); //create instance
d.inputCourseCode(); //call method
}
inputCourseCode is a method of DialogBox class, you need a reference to an instance of that class to call it.
If you need to call that function without an istance class you need to declare it as static:
public static String inputCourseCode() {
String input = JOptionPane.showInputDialog("Input the course code of this course:");
return input;
}
Then you can call it from main without create an object:
public static void main (String arg[]) {
String inputCourseCode;
DialogBox.inputCourseCode(); //call static method
}
new DialogBox().inputCourseCode();
You need to instantiate your class to access non-static members.
See Java Tutorial: Understanding Instance and Class Members
Well it depends on your need.
If you want it to be tied at class level, then just make it static and remove 'this' from this.inputCourseCode() in the current code and it will work.
If you want it to be part of each object then you need to create object of DialogBox and call it explicitly as follows :
DialogBox dialogBox = new DialogBox();
dialogBox.inputCourseCode();