This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
This is my code:
public class LoginSql {
Conexion con = new Conexion();
con.conexionDB(); // error here <
}
package con does not exist
but exist
public class Conexion {
public Connection conexionDB(){
Connection link = null;
return link
}
}
I using package example
com.hi.pro
com.hi.pro.class
com.hi.pro.sql
I have code in com.hi.pro.sql but not work
only work in the com.hi.pro in class app.java
The problem is that this statement:
con.conexionDB();
... is just a method call statement. That can't sit directly inside a class - it needs to be in a method, constructor, or initializer block. You probably want to put it in a constructor:
public class LoginSql {
private Conexion con = new Conexion();
public LoginSql() {
con.conexionDB();
}
}
The declaration of con is fine, because that's an instance variable declaration, which is allowed directly inside a class declaration.
Note that this has nothing to do with packages. It's purely a matter of trying to put a statement in the wrong place.
call that method inside some method or constructor.
like
public void createConnection(){
con.conexionDB();
}
OR
public LoginSql(){
con.conexionDB();
}
Hope that helps
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I would like to inherrit the rooms variable just as foo is being inherited. I have searched around google and stackoverflow but couldn't find relevant posts. It may be a short description of the question but I don't think anything more is needed.
Code
import java.util.ArrayList;
class HomeAbstraction { // HomeAbstraction = Rooms; It's named like this to fit the rest of the project
float area;
public HomeAbstraction(float area) {
this.area = area;
}
}
class Home {
String foo = "bar";
ArrayList < HomeAbstraction > rooms = new ArrayList < HomeAbstraction > ();
// setter
public void setRooms(HomeAbstraction rooms) {
this.rooms.add(rooms);
}
}
class Department extends Home {
// constructor
public Department() {
foo = "foo"; // works fine
rooms; // should be pulled from parent but isn't working
rooms.setRooms(new HomeAbstraction(50)); // Not sure if I can access the setter from this child class
}
}
Error log
idk.java:25: error: cannot find symbol
rooms.setRooms(new HomeAbstraction(50));
^
symbol: method setRooms(HomeAbstraction)
location: variable rooms of type ArrayList<HomeAbstraction>
1 error
You can try:
rooms.add(new HomeAbstraction(50));
or
super.setRooms(new HomeAbstraction(50));
or
super.rooms.add(new HomeAbstraction(50));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to learn object oriented code in Java, and am following a tutorial. I am currently stuck trying to parse a string into my class. It is returning the following error:
Name cannot be resolved to a variable
I have a main file, called start.java, and the class I am trying to call is in a different file, called phone.java. Both are in a folder called src. Below is the start.java code (which is throwing the error)
package src;
public class Start {
public static void main(String[] args){
phone android = new phone(Name:"android 10");
System.out.println(android.getName());
}
}
And here is the class I am trying to call, in phone.java
package src;
public class phone{
private String name;
public phone(String name) {
this.name = name;
}
public String getName(){
return this.name;
}
}
Much thanks for your help
You need to remove the Name from new phone(Name:"android 10") and need to use new phone("android 10").
You just need to pass the value for the name, your constructor will bind it to the name variable.
Refer below code
public class Start {
public static void main(String[] args){
phone android = new phone("android 10");
System.out.println(android.getName());
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm new to this so sorry if i don't explain it great.
I have 2 class Interface and Depot.
I can call depot1.getName() and depot2.getName() in Instance class.
But I can't call them in Depot class as I am trying to check no instance of that class has the entered name:(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
Could it be because depot1 and depot2 haven't been created yet?
Here is getName
public String getName(){
return name;
}
I think it may have to do with the fact that depot2 may not exist so i tried:
(depot2 != null && tempname.equals(depot2.getName()))
but that still gives more erors and won't let me compile
I am getting the following error "cannot find symbol - variable depot1"
Can I use isInstance? https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance%28java.lang.Object%29
Any help would be greatly appreciated, Thanks
Looks like your code don't have instance of depot1 create a instance using new like following snippet: This is not exact answer but it will help you.
package com.test;
class Depot
{
private String name;
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
}
public class Test
{
public static void main( String[] args )
{
Depot depot1 = new Depot();
depot1.setName( "depot1" );
Depot depot2 = new Depot();
depot2.setName( "depot2" );
// Rest of code
//(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Can someone please explain how is possible, that method obtain(..) throws IllegalStateException for input ConfiguratorType.SKODA (the variable configurators contains {SKODA=null})? How can it be null, I do not understand why SkodaConfigurator.INSTANCE returns null. It should never be null or am i mistaken? The code is executed in servlet environment, Java 7.
Thank you
public class CarConfigurators {
private static Map<ConfiguratorType, CarConfigurator> configurators
= new EnumMap<ConfiguratorType, CarConfigurator>(ConfiguratorType.class);
static {
configurators.put(ConfiguratorType.SKODA, SkodaConfigurator.INSTANCE);
// ..
}
public static CarConfigurator obtain(ConfiguratorType type) {
CarConfigurator configurator = configurators.get(type);
if (configurator == null)
throw new IllegalStateException("Car configurator of type " + type + " is not registered.");
return configurator;
}
...
}
public class SkodaConfigurator extends CarConfigurator {
public static final SkodaConfigurator INSTANCE = new SkodaConfigurator();
...
}
public enum ConfiguratorType {
SKODA,
// ..
}
Static code cannot all run simultaneously, the various bits of static initialization going on have to happen in a given order. Clearly in this case, your static block which does configurations.put(...) is running before the static variable in SkodaConfiguration is initialized.
This is related to static initialization order.
I found this from another answer
public class Main {
{
System.out.printf("NON-STATIC BLOCK\n");
}
static{
System.out.printf("STATIC BLOCK\n");
}
public static Main m = new Main();
public Main(){
System.out.printf("MAIN CONSTRUCTOR\n");
}
public static void main(String... args) {
//Main m = new Main();
System.out.printf("MAIN METHOD\n");
}
}
Output :
STATIC BLOCK
NON-STATIC BLOCK
MAIN CONSTRUCTOR
MAIN METHOD
Please go through this : Java Static Initialization Order
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a small problem in my java code. Error is
Syntax error on token ";", , expected
Here is my code:
package natchly.chest;
import natchly.chest.blocks.BlockStoneChest;
import net.minecraft.block.Block;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.event.FMLInitializationEvent;
#Mod(modid="chestsplus", name="Chests+", version="1.4.6_01")
#NetworkMod(clientSideRequired=true, serverSideRequired=false)
public class ModChests {
public int idBlockStoneChest = 250;
public static Block blockStoneChest; // <-------- Error here
blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
#Init
public void init(FMLInitializationEvent e) {
GameRegistry.registerBlock(blockStoneChest);
LanguageRegistry.addName(blockStoneChest, "Stone Chest");
}
}
Either do this:
public static Block blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
Or this:
public static Block blockStoneChest; <-------- Error here
static {
blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);
}
Combine these two lines into one declaration and instantiation step. The way you're doing it isn't permitted in Java unless that's inside of a method.
public static BlockStoneChest blockStoneChest = new BlockStoneChest(idBlockStoneChest).setBlockName("blockNAZWABLOKU").setHardness(1.5F).setResistance(5.0F);