Yes, something like:
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
That will only work for a single string parameter of course, but you can modify it pretty easily.
Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:
package foo;
public class Outer
{
public static class Nested {}
}
To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").
Yes, something like:
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
That will only work for a single string parameter of course, but you can modify it pretty easily.
Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:
package foo;
public class Outer
{
public static class Nested {}
}
To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").
You can use Class.forName() to get a Class object of the desired class.
Then use getConstructor() to find the desired Constructor object.
Finally, call newInstance() on that object to get your new instance.
Class<?> c = Class.forName("mypackage.MyClass");
Constructor<?> cons = c.getConstructor(String.class);
Object object = cons.newInstance("MyAttributeValue");
Videos
Create an instance of that class with the 'new' keyword in your main activity and then invoke those methods on that object.
BDD bdd = new BDD();
boolean value = bdd.insertarGuia("foo", "bar");
Or make the methods static
Change
public boolean insertarGuia(String g, String d) {
to
public static boolean insertarGuia(String g, String d) {
and in your main method have
boolean value = BDD.insertaGuia("foo", "bar");
It's very easy, just create an object of your class and call any of the method in your activity:
BDD obj = new BDD();
obj.ArregloGuias();
I read Java tutorial and it says " An object is an instance of a class" i google stackoverflow and still dont 100% sure
lets say i create an object called Dog from class Main
what is instance and what is an object here?
Main Dog = new Main()
It's called polymorphism:
The instance of a class can be treated as instance of that class but also as an instance of every interface the class implements, since it has to implement all the interface's methods. An interface is a contract that says: "The class implementing me offers at least the same operations that I offer."
Every HashMap is a Map, but not every Map is a HashMap.
You cannot create an instance of an interface, because an interface is basically an abstract class without the restriction against multiple inheritance.
And an abstract class is missing parts of its implementation, or is explicitly marked as abstract to prohibit instantiation.
What you can do, and in your example did, is instantiating a class derived from (called implementing) said interface, and upcasting it to any base, be it interface, concrete class or abstract class.
Unless the implementer foolishly broke the contract, the Liskov substitution principle applies and it works just like you actually had instantiated the interface.
So, I'm a beginner currently learning about objects and constructors in Java. One term that occurs a lot but I can't understand(after a lot of thinking) is 'instance'. I've heard conflicting explanations, some saying that objects and instances are the same thing and some saying that a line of code like 'Car a = new Car();' is a example of an instance. So what exactly is an instance and what is it's relationship to constructors?